MergeSort
MergeSort
h>
#include<stdlib.h>
#include<math.h>
int N;
int main() {
int n, i, ch, a[N], x;
printf("Enter choice: 1.Correctness 2.Complexity - ");
scanf("%d", &ch);
switch(ch) {
case 1: {
printf("Enter the size of array: ");
scanf("%d", &n);
N = n;
printf("Enter %d array elements -\n", n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
x = MergeSort(a, n);
printf("Sorted array is:\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
break;
}
case 2: {
int arr[32000], size, i, j, t1, t2, t3;
float ln;
printf("Enter size of array for complexity calculation of array of
its next 5 multiples: ");
scanf("%d", &size);
printf("Size\tAscending\tcnlog(n)\tDescending\tcnlog(n)\tRandom\tc
nlog(n)\n");
for(i = 1; i <= 5; i++, size *= 2) {
N = size;
ln = 2 * size * log(size) / log(2);
for(j = 0; j < size; j++)
arr[j] = j;
t1 = MergeSort(arr, size);
for(j = 0; j < size; j++)
arr[j] = size - j;
t2 = MergeSort(arr, size);
for(j = 0; j < size; j++)
arr[j] = rand() % 32000;
t3 = MergeSort(arr, size);
printf("%d\t%d\t\t%.0f\t\t%d\t\t%.0f\t\t%d\t%.0f\n", size,
t1, ln, t2, ln, t3, ln);
}
printf("(Considering c as 2 here)\n");
break;
}
default: {
exit(0);
}
}
}
c1 = MergeSort(b, n/2);
c2 = MergeSort(c, n - (n/2));
c3 = Merge(b, n/2, c, n - (n/2), a);