QUICK SORT with c-program
Aim : To Implement Quick Sort Using C program
Program:
#include<stdio.h>
int a[50];
void main()
{
int i,n;
clrscr();
printf(" Enter no of elements to be entered");
scanf("%d",&n);
printf("\n Enter elements");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
quicksort(1,n);
printf("\nElements after sorting");
for(i=1;i<=n;i++)
printf("%d ",a[i]);
getch();
}
int quicksort(int low,int high)
{
int j;
if(low<high)
{
j=partition(low,high);
quicksort(low,j-1);
quicksort(j+1,high);
}
return;
}
int partition(int low,int high)
{
int i,j,temp,pivot;
i=low;
j=high+1;
pivot=a[low];
while(i<j)
{
do
{
i=i+1;
}while(i<high && pivot>=a[i]);
do
{
j=j-1;
}while(j>low && pivot<=a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[low]=a[j];
a[j]=pivot;
return j;
}
Output:
No comments:
Post a Comment