-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge_sort.c
81 lines (66 loc) · 1.53 KB
/
Merge_sort.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
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
void Merge(int *arr, int start, int mid, int end){
int temp[end - start + 1];
int i = start, j = mid + 1, k = 0;
//traversing both arrays comparing element and adding to temp array
while (i <= mid && j <= end)
{
if (arr[i] <= arr[j])
{
temp[k] = arr[i];
k++;
i++;
}
else
{
temp[k] = arr[j];
k++;
j++;
}
}
//adding the remaining element to array
while (i <= mid)
{
temp[k] = arr[i];
k++;
i++;
}
while (j <= end)
{
temp[k] = arr[j];
k++;
j++;
}
// printf("%d - %d\n", start, end);
// for (int i = 0; i < sizeof temp / sizeof temp[0]; i++)
// {
// printf("%d\n",temp[i]);
// //arr[i] = temp[i];
// }
// printf("=========================\n");
for (i = start; i <= end ; i++)
{
arr[i] = temp[i - start];
}
}
void MergeSort(int *arr, int start, int end){
printf("%d - %d\n", start, end);
if (start < end)
{
int mid = (end + start) / 2;
MergeSort(arr, start, mid);
MergeSort(arr, mid + 1, end);
//Merge(arr, start, mid, end);
}
}
int main(){
int a[] = {90,4,5,82,1,2,0,43};
int sizeof_arr = sizeof a / sizeof a[0];
MergeSort(a, 0, sizeof_arr-1);
// for (int i = 0; i < sizeof_arr ; i++)
// {
// printf("%d\n", a[i]);
// }
return 0;
}