-
Notifications
You must be signed in to change notification settings - Fork 1
/
msrec.c
73 lines (66 loc) · 1.33 KB
/
msrec.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
#include<stdio.h>
void merge(int A[], int mid, int low, int high)
{
int i, j, k, B[100];
i = low;
j = mid + 1;
k = low;
while (i <= mid && j <= high)
{
if (A[i] < A[j])
{
B[k] = A[i];
i++;
k++;
}
else
{
B[k] = A[j];
j++;
k++;
}
}
while (i <= mid)
{
B[k] = A[i];
k++;
i++;
}
while (j <= high)
{
B[k] = A[j];
k++;
j++;
}
for (int i = low; i <= high; i++)
{
A[i] = B[i];
}
}
void mergeSort(int A[], int low, int high)
{
int mid;
if(low<high){
mid = (low + high) /2;
mergeSort(A, low, mid);
mergeSort(A, mid+1, high);
merge(A, mid, low, high);
}
}
void main()
{
int i,j,s;
printf("Enter the size of the array: ");
scanf("%d",&s);
int arr[s];
printf("Enter the numbers to be sorted using Merge Sort in the array below:-\n");
for(i=0;i<s;i++)
scanf("%d",&arr[i]);
printf("Unsorted array: ");
for (i = 0; i < s; i++)
printf("%d ", arr[i]);
mergeSort(arr,0,s-1);
printf("\nSorted array: ");
for (i = 0; i < s; i++)
printf("%d ", arr[i]);
}