-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion3.c
77 lines (59 loc) · 1.37 KB
/
question3.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
69
70
71
72
73
74
75
76
77
/*
MERGE SORT
Space complexity: stack O(logn) + space for merge procedure O(n)
Therefore total = O(n)
Time complexity: time taken to merge O(n) + time taken to sort by masters theorem O(nlogn)
Therefore: O(nlogn)
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void merge(int *arr, int start, int mid, int end) {
int len1 = mid - start + 2;
int len2 = end - mid + 1;
int left[len1], right[len2];
int i;
for(i=0; i<len1-1; i++) {
left[i] = arr[start + i];
}
for(i=0; i<len2-1; i++) {
right[i] = arr[mid + i + 1];
}
left[len1-1] = INT_MAX;
right[len2-1] = INT_MAX;
int k;
int j;
i=0,j=0;
for(k=start; k<=end; k++) {
if(left[i] < right[j]) {
arr[k] = left[i];
i++;
}else{
arr[k] = right[j];
j++;
}
}
}
void mergeSort(int *arr, int start, int end) {
int mid;
if(start < end) {
mid = (start + end)/2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
}
void display(int *arr, int size) {
int i;
for(i=0;i<size;i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {9,6,5,0,8,2};
int size = sizeof(arr)/sizeof(arr[0]);
mergeSort(arr,0,size-1);
display(arr, size);
return 0;
}