ALDS1_5_B-MergeSort.
Description:
Write a program of a Merge Sort algorithm implemented by the following pseudocode. You should also report the number of comparisons in the Merge function.
Merge(A, left, mid, right)
n1 = mid - left;
n2 = right - mid;
create array L[0...n1], R[0...n2]
for i = 0 to n1-1
do L[i] = A[left + i]
for i = 0 to n2-1
do R[i] = A[mid + i]
L[n1] = SENTINEL
R[n2] = SENTINEL
i = 0;
j = 0;
for k = left to right-1
if L[i] <= R[j]
then A[k] = L[i]
i = i + 1
else A[k] = R[j]
j = j + 1
Merge-Sort(A, left, right){
if left+1 < right
then mid = (left + right)/2;
call Merge-Sort(A, left, mid)
call Merge-Sort(A, mid, right)
call Merge(A, left, mid, right)
In the first line n is given. In the second line, n integers are given.
Output:
In the first line, print the sequence S. Two consequtive elements should be separated by a space character.
In the second line, print the number of comparisons.
Constraints:
n ≤ 500000
0 ≤ an element in S ≤ 109
10
8 5 9 2 6 3 7 1 10 4
SampleOutput:
1 2 3 4 5 6 7 8 9 10
34
Codes:
//#define LOCAL
#include <cstdio>
#define M 500010
#define SENTINEL 2000000000
int s = 0, A[M], L[M], R[M];
void merge(int l, int m, int r) {
int i, j, k, n1 = m-l, n2 = r-m;
L[n1] = R[n2] = SENTINEL;
for(i=0; i<n1; ++i) L[i] = A[l+i];
for(i=0; i<n2; ++i) R[i] = A[m+i];
i = j = 0;
for(k=l; k<r; ++k) {
if(L[i] <= R[j]) A[k] = L[i++];
else A[k] = R[j++]; ++s;
}
}
void mergeSort(int l, int r) {
if(l+1 < r) {
int m = (l+r)/2;
mergeSort(l, m);
mergeSort(m, r);
merge(l, m, r);
}
}
int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif
int i, n;
scanf("%d", &n);
for(i=0; i<n; ++i) scanf("%d", &A[i]);
mergeSort(0, n);
for(i=0; i<n; ++i) {
if(i) printf(" ");
printf("%d", A[i]);
}
printf("\n%d\n", s);
return 0;
}
ALDS1_6_B-Partition.
Description:
Quick sort is based on the Divide-and-conquer approach. In QuickSort(A, p, r), first, a procedure Partition(A, p, r) divides an array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, inturn, less than or equal to each element of A[q+1..r]. It also computes the index q.
In the conquer processes, the two subarrays A[p..q-1] and A[q+1..r] are sorted by recursive calls of QuickSort(A, p, q-1) and QuickSort(A, q+1, r).
Your task is to read a sequence A and perform the Partition based on the following pseudocode:
Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4 do if A[j] <= x
5 then i = i+1
6 exchange A[i] and A[j]
7 exchange A[i+1] and A[r]
8 return i+1
The first line of the input includes an integer n, the number of elements in the sequence A.
In the second line, n elements of the sequence are given separated by space characters.
Output:
Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character. The element which is selected as the pivot of the partition should be indicated by [ ].
Constraints:
1 ≤ n ≤ 100,000
12
13 19 9 5 12 8 7 4 21 2 6 11
SampleOutput:
9 5 8 7 4 2 6 [11] 21 13 19 12
Codes:
//#define LOCAL
#include <cstdio>
#define M 100010
int A[M];
int partition(int p, int r) {
int i = p-1, a = A[r], j, t;
for(j=p; j<r; ++j) {
if(A[j] <= a) {
++i;
t = A[i]; A[i] = A[j]; A[j] = t;
}
}
A[r] = A[i+1]; A[i+1] = a;
return i+1;
}
int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt&q