-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort.c
61 lines (51 loc) · 1.07 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
#include <stdio.h>
#include <time.h>
#define MAX_SIZE 10
void printArray(char* name, int arr[], int size) {
printf("%s[%d] : {", name, size);
for (int i=0; i<size; i++) {
printf("%d", arr[i]);
if (i+1!=size)
printf(", ");
}
printf("}\n");
}
void arraySetup(int arr[], int size) {
srand(time(NULL));
for (int i=0; i<size; i++) {
arr[i] = rand()%100;
}
}
void merge(int a[], int start, int middle, int end) {
int b[end-start+1];
int i = start;
int j = middle+1;
for (int k=0; k<=(end-start); k++) {
if ((i<=middle)&&(j>end||a[i]<=a[j])) {
b[k] = a[i];
i++;
}
else {
b[k] = a[j];
j++;
}
}
for (int k=0; (k<=end-start); k++) {
a[k+start] = b[k];
}
}
void mergeSort(int a[], int start, int end) {
if ((end - start) < 1)
return;
int middle = (start+end)/2;
mergeSort(a, start, middle);
mergeSort(a, middle+1, end);
merge(a, start, middle, end);
}
void main(int argc, char* argv[]) {
int arr[MAX_SIZE];
arraySetup(arr, MAX_SIZE);
printArray("arr", arr, MAX_SIZE);
mergeSort(arr, 0, MAX_SIZE-1);
printArray("arr", arr, MAX_SIZE);
}