-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_sort_prac1.c
89 lines (68 loc) · 1.62 KB
/
merge_sort_prac1.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
82
83
84
85
86
87
88
89
#include <stdio.h>
#define MAX 1000
int arr[MAX];
int num;
void Merge(int arr[], int leftarr[], int rightarr[], int num,
int leftarrsize, int rightarrsize)
{
int k = 0; /* Left*/
int l = 0; /* Right */
int m = 0; /* Main arr */
/* Swap */
while(k < leftarrsize && l < rightarrsize && m < num) {
if (leftarr[k] < rightarr[l]) {
arr[m] = leftarr[k];
m++;
k++;
} else {
arr[m] = rightarr[l];
m++;
l++;
}
}
while(k < leftarrsize) {
arr[m] = leftarr[k];
k++;
m++;
}
while(l < rightarrsize) {
arr[m] = rightarr[l];
l++;
m++;
}
}
void MergeSort(int arr[], /*int start, int end,*/ int num)
{
if (num != 1) {
int z;
int k, l=0, m = 0;
int mid = num/2;
int leftarr[mid];
int rightarr[num - mid];
for(k=0; k <mid; k++) {
leftarr[l] = arr[k];
l++;
}
for(k=mid; k <num; k++) {
rightarr[m] = arr[k];
m++;
}
MergeSort(leftarr, /*start, start+mid -1,*/ mid);
MergeSort(rightarr, /*start + (end-start)/2, end,*/ num-mid);
Merge(arr, leftarr, rightarr, num, mid, num-mid);
}
}
int main()
{
int k;
printf("Welcome to Merge Sort algorithm...\n");
printf("Enter number of elements in Array..\n");
scanf("%d", &num);
for(k=0; k < num; k++) {
scanf("%d", &arr[k]);
}
MergeSort(arr, /*0, num-1,*/ num);
for(k=0; k < num; k++) {
printf("[%d]", arr[k]);
}
}