-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeargeSort.c
44 lines (36 loc) · 1013 Bytes
/
MeargeSort.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
//merge_sort
/* Merge sort in C */
#include<stdio.h>
#include<stdlib.h>
void Merge(int *A,int *L,int leftCount,int *R,int rightCount) {
int i,j,k;
i = 0; j = 0; k =0;
while(i<leftCount && j< rightCount) {
if(L[i] < R[j]) A[k++] = L[i++];
else A[k++] = R[j++];
}
while(i < leftCount) A[k++] = L[i++];
while(j < rightCount) A[k++] = R[j++];
}
void MergeSort(int *A,int n) {
int mid,i, *L, *R;
if(n < 2) return;
mid = n/2;
L = (int*)malloc(mid*sizeof(int));
R = (int*)malloc((n- mid)*sizeof(int));
for(i = 0;i<mid;i++) L[i] = A[i];
for(i = mid;i<n;i++) R[i-mid] = A[i];
MergeSort(L,mid);
MergeSort(R,n-mid);
Merge(A,L,mid,R,n-mid);
free(L);
free(R);
}
int main() {
int A[] = {6,2,3,1,9,10,15,13,12,17};
int i,numberOfElements;
numberOfElements = sizeof(A)/sizeof(A[0]);//good
MergeSort(A,numberOfElements);
for(i = 0;i < numberOfElements;i++) printf("%d ",A[i]);
return 0;
}