Merge Sort (Non Recursive) > C Program

Analysis of Algorithm

Merge Sort (Non Recursive) > C Program


#include<stdio.h>
#include<conio.h>
void merge(int x[],int lb1,int ub1,int ub2)
{
int temp[50],i,j,k;
i=lb1;
j=ub1+1;
k=0;
while(i<=ub1 && j<=ub2)
 if(x[i]<x[j])
  temp[k++]=x[i++];
 else
  temp[k++]=x[j++];
while(i<=ub1)
 temp[k++]=x[i++];
while(j<=ub2)
 temp[k++]=x[j++];
for(i=lb1,j=0;i<=ub2;i++,j++)
 x[i]=temp[j];
}
void main()
{
int A[100],n,i,x;
clrscr();
printf("Enter the number of elements in the array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
 printf("Enter element %d: ",i+1);
 scanf("%d",&A[i]);
}
printf("Enter the element where you want to divide the array:");
scanf("%d",&x);
merge(A,0,x,n-1);
printf("The sorted array is :\n");
for(i=0;i<n;i++)
printf("%d ",A[i]);
getch();
}

Comments

Popular posts from this blog

Intermediate Code Generation > C Program