-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.c
66 lines (55 loc) · 1.26 KB
/
mergesort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
main()
{
int data[100], i, j, n, temp;
/* INPUT */
printf("Give n:");
scanf("%d", &n);
printf(" n = %d \n", n);
for (i = 0; i < n; i++)
scanf("%d", data+i);
printf(" \n Numbers read are: ");
for (i=0;i<n; i++)
printf("%d ", *(data+i));
printf(" \n");
/* CALL */
mergesort(data, 0, n-1);
/* PRINT RESULTS */
printf(" \n Sorted numbers are: ");
for (i=0; i<n; i++) printf("%d ", data[i]);
printf(" \n");
}
mergesort(a, i, j) /* merge sort routine */
int a[], i, j;
{
int mid;
printf(" Entered with i = %d, j = %d \n", i, j);
if (i >= j) return;
mid = (i + j)/2;
mergesort(a, i, mid);
mergesort(a, mid +1, j);
merge(a, i, j);
}
merge (a, i, j) /* takes in parameters as a,
int a[], i, j;
{
int k, b[100], mid, l, start; /* b is temp array to store elements during merge sorting, l is a temp variable*/
start = i;
mid = (i+j)/2;
k = mid +1;
l = i;
/* FORM ARRAY b - array b will start from l */
while (i<=mid && k <=j)
if (a[i] >= a[k])
b[l++] = a[i++];
else
b[l++] = a[k++];
if (i > mid)
for( ; k <=j ; ) b[l++] = a[k++];
else
if (k > j)
for (; i<=mid;) b[l++] = a[i++];
/* copy back after sorting, from array b to array a */
for (l = start; l <=j; l++)
a[l] = b[l];
}