-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMerge_sort_iterative.c
74 lines (74 loc) · 1.61 KB
/
Merge_sort_iterative.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
#include <stdio.h>
#include <stdlib.h>
void merge(int A[], int l, int mid, int h)
{
int i = l, j = (mid + 1);
int *b;
int k = l;
b = (int *)malloc((h + 1) * sizeof(int));
while (i <= mid && j <= h)
{
if (A[i] < A[j])
{
b[k] = A[i];
k++;
i++;
}
else if (A[j] < A[i])
{
b[k] = A[j];
k++;
j++;
}
}
for (; i <= mid; i++)
{
b[k] = A[i];
k++;
}
for (; j <= h; j++)
{
b[k] = A[j];
k++;
}
for (int i = l; i <= h; i++)
{
A[i] = b[i];
}
}
void merge_sort(int A[], int n)
{
int p, i, l, mid, h;
for (p = 2; p <= n; p = (2 * p)) // passes
{
for (i = 0; i + p - 1 < n; i = i + p)
{
l = i;
h = (i + p - 1);
mid = (l + h) / 2;
merge(A, l, mid, h);
}
if ((n - i) > (p / 2)) /*Merging each element during each pass.*/
{
l = i;
h = i + p - 1;
mid = (l + h) / 2;
merge(A, l, mid, (n - 1)); /*as 'if' condition will be executed only
once so, instead of 'h' we will write (n-1) so,that all the elements
till the end are merged.*/
}
}
if (n > (p / 2))
{
merge(A, 0, (p / 2) - 1, (n - 1)); /*same reason as above*/
}
}
int main()
{
int A[] = {11, 5, 14, 2, 6, 3, 1};
merge_sort(A, 7);
for (int i = 0; i < 7; i++)
{
printf("%d ", A[i]);
}
}