0% found this document useful (0 votes)
9 views2 pages

Merge Sort

Uploaded by

Apoorv Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Merge Sort

Uploaded by

Apoorv Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#define MAX 100

void merge(int array[], int low, int mid, int high)


{
int temp[MAX];
int i = low;
int j = mid + 1;
int k = low;
while ((i <= mid) && (j <= high))
{
if (array[i] <= array[j])
temp[k++] = array[i++];
else
temp[k++] = array[j++];
} /*End of while*/
while (i <= mid)
temp[k++] = array[i++];

while (j <= high)


temp[k++] = array[j++];

for (i = low; i <= high; i++)


array[i] = temp[i];

} /*End of merge()*/

void merge_sort(int array[], int low, int high)


{
int mid;
if (low != high)
{
mid = (low + high) / 2;
merge_sort(array, low, mid);
merge_sort(array, mid + 1, high);
merge(array, low, mid, high);
}
} /*End of merge_sort*/
int main()
{
int i, n;
int array[MAX];

printf("Enter the number of elements : ");


scanf("%d", & n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ", i + 1);
scanf("%d", & array[i]);
}

printf("Unsorted list is :\n");


for (i = 0; i < n; i++)
printf("%d ", array[i]);

merge_sort(array, 0, n - 1);

printf("\nSorted list is :\n");


for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");

return 0;
} /*End of main()*/

You might also like