-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort1.c
More file actions
60 lines (45 loc) · 875 Bytes
/
sort1.c
File metadata and controls
60 lines (45 loc) · 875 Bytes
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
#include<stdio.h>
static void merge(int a[],int start,int mid,int end) {
int* temp1 = (int)malloc(sizeof(int * (mid-start)));
int* temp = (int)malloc(sizeof(int * (end-start)));
int temp2[] = (int)malloc(sizeof(int * (end-mid)));
int i = start;
int j = mid;
int k = 0;
while(i < mid && j < high) {
if(s[i] <= s[j]) {
temp[k] = s[i];
i++;
}else {
temp[k] = s[j];
j++;
}
k++;
}
while(i < mid) {
temp[k++] = s[i];
i++;
}
while(j < high) {
temp[k++] = s[j];
j++;
}
int len = end - start;
k =0;
for(k=0;k< (end-start);k++) {
s[k] = temp[k];
}
}
static void merge_sort(int a[],int start,int end) {
int mid;
if(start < end) {
mid = start + (end-start)/2;
merge_sort(a,start,mid);
merge_sort(a,mid+1,end);
merge(a,start,mid,end);
}
}
int main() {
int a[] = {5,2,12,1,212,10,6};
merge_sort(a,0,6);
}