0% found this document useful (0 votes)
105 views

21.1 185. Iterative Merge Sort PDF

This document describes the merge sort algorithm. It includes functions for merging sorted subarrays, recursively splitting the array into halves and merging them back together, and a main function that tests the algorithm on a sample array. The key steps are: (1) recursively divide the array into single elements, (2) merge the subarrays back together by comparing elements in order, (3) repeat merging until the full array is sorted.

Uploaded by

Md Asif Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

21.1 185. Iterative Merge Sort PDF

This document describes the merge sort algorithm. It includes functions for merging sorted subarrays, recursively splitting the array into halves and merging them back together, and a main function that tests the algorithm on a sample array. The key steps are: (1) recursively divide the array into single elements, (2) merge the subarrays back together by comparing elements in order, (3) repeat merging until the full array is sorted.

Uploaded by

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

Merge Sort

#include <stdio.h>
#include<stdlib.h>

void swap(int *x,int *y)


{
int temp=*x;
*x=*y;
*y=temp;
}

void Merge(int A[],int l,int mid,int h)


{
int i=l,j=mid+1,k=l;
int B[100];

while(i<=mid && j<=h)


{
if(A[i]<A[j])
B[k++]=A[i++];
else
B[k++]=A[j++];
}
for(;i<=mid;i++)
B[k++]=A[i];
for(;j<=h;j++)
B[k++]=A[j];

for(i=l;i<=h;i++)
A[i]=B[i];
}

void IMergeSort(int A[],int n)


{
int p,l,h,mid,i;

for(p=2;p<=n;p=p*2)
{
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(p/2<n)
Merge(A,0,p/2-1,n);

int main()
{
int A[]={11,13,7,12,16,9,24,5,10,3},n=10,i;

IMergeSort(A,n);

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

return 0;
}

You might also like