-
Notifications
You must be signed in to change notification settings - Fork 0
/
InPlaceMergeSort.c
55 lines (53 loc) · 1.08 KB
/
InPlaceMergeSort.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
#include<stdio.h>
#include<conio.h>
#define SIZE 100
void merge(int arr[],int p,int q,int r)
{
int i,j,last;
for(i=r;i>q;i--)
{
last=arr[q];
for(j=q-1;j>=0 && arr[j]>arr[i];j--)
arr[j+1]=arr[j];
if(j!=q-1 && last>arr[i])
{
arr[j+1]=arr[i];
arr[i]=last;
}
}
}
void mergesort(int arr[],int low,int high)
{
if(low>=high)
return;
int mid=(low+high)/2;
mergesort(arr,low,mid);
mergesort(arr,mid+1,high);
merge(arr,low,mid,high);
}
void Display(int arr[],int n)
{
printf("The array is\n");
int i;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
void main()
{
int arr[SIZE];
int n;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Initializing the array\n");
int i;
for(i=0;i<n;i++)
arr[i]=rand()%(n+1);
Display(arr,n);
printf("Sorting the array\n");
mergesort(arr,0,n-1);
Display(arr,n);
getch();
}