-
Notifications
You must be signed in to change notification settings - Fork 2
/
mergeSort.c
70 lines (58 loc) · 2.11 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
64
65
66
67
68
/*
* MERGE-SORT(A,p,r)
* if p < r
* then q <- floor((p+r)/2)
* MERGE-SORT(A,p,q)
* MERGE-SORT(A,q+1,r)
* MERGE(A,p,q,r)
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void printarray(int *ptr, int firstIndex, int lastIndex){
int i ;
printf("\n-------------------------------------------------------------------\n");
for(i = firstIndex; i <= lastIndex; i++ ){
printf("%d ", *(ptr+i)) ;
}
printf("\n") ;
}
void merge(int *ptr, int firstIndex, int mid, int lastIndex){
/* *(ptr+firstIndex) to *(ptr+mid) is already sorted
* *(ptr+mid+1) to *(ptr+lastIndex) is already sorted
* just merge these two lists so that the resulting list is also sorted
*/
int *l1, *l2 ;
long unsigned int i=0,j=0,k=0, count1 = mid - firstIndex, count2 = lastIndex - mid + 1, count3 = count1 + count2 ;
int *l3 = (int*)calloc(count3, sizeof *ptr) ;
l1 = ptr + firstIndex ;
l2 = ptr + mid ;
while(i < count1 && j < count2 ){
*(l3 + k++) = *(l1 + i) <= *(l2 + j) ? *(l1 + i++) : *(l2 + j++) ;
}
if(i < count1){ memmove(l3 + k, l1 + i, (count1 - i) * sizeof *l1) ; } ;
if(j < count2){ memmove(l3 + k, l2 + j, (count2 - j) * sizeof *l2) ; } ;
//copy the items of l3 to ptr, to the appropriate block
memmove(ptr+firstIndex, l3, count3 * sizeof *l3 ) ; //efficient than for loop to copy
free(l3) ; //l3 was a temporary list, which needs to be freed
}
void mergesort(int *ptr, int firstIndex, int lastIndex){
int mid;
if (firstIndex < lastIndex) {
mid = (firstIndex + lastIndex)/2 ;
mergesort(ptr, firstIndex, mid) ;
mergesort(ptr, mid+1, lastIndex) ;
merge(ptr, firstIndex, mid+1, lastIndex) ;
}
}
int main(){
int alist[15] = {2,190,10,-1,29,24,3,120, 31, 2, 0, 9,21,2,65} ;
int *lptr = alist;
printf("\nBefore sorting array was: \n") ;
printarray(lptr, 0, (int)(sizeof alist/sizeof *lptr)-1 );
mergesort(lptr, 0, (int)(sizeof alist/sizeof *lptr)-1 ) ;
printf("\nAfter calling mergesort sort procedure \n") ;
printarray(lptr, 0, (int)(sizeof alist/sizeof *lptr)-1 );
return 0;
}