-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.c
69 lines (67 loc) · 1.6 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
69
#include <stdio.h>
void printLoop(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}
void merge(int *arr, int lower, int mid, int upper)
{
// temp array creation to store sorted results
int TempLength = upper - lower + 1;
int temp[TempLength];
int i = lower; // pointer for left sublist
int j = mid + 1; // pointer for right sublist
int k = 0; // pointer for temp array
while (i <= mid && j <= upper)
{
if (arr[i] <= arr[j])
{
temp[k] = arr[i];
i++;
}
else{
temp[k] = arr[j];
j++;
}
k++;
}
if(i>mid){
while(j<=upper){
temp[k] = arr[j];
k++;
j++;
}
}else{
while(i<=mid){
temp[k] = arr[i];
k++;
i++;
}
}
k = 0; // making k index to be zero again for traversing it from the first element
for (int s = lower; s <= upper; s++)
{
arr[s] = temp[k];
k++;
}
}
void mergeSort(int *arr, int lower, int upper)
{
if (lower < upper) // means atleast two elements are there
{
int mid = (lower + upper) / 2;
mergeSort(arr, lower, mid); // left division
mergeSort(arr, mid + 1, upper); // right division
merge(arr, lower, mid, upper); // merging left and right sublists
}
}
int main()
{
int arr[] = {5, 6,3,9,4, 1, 1, 2, 7, 4};
int size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, size - 1);
printLoop(arr, size);
return 0;
}