C Program to perform Quick Sort:
#include<stdio.h>
void quicksort(int a[],int,int);
void main()
{
int c,l=0,h=4,i,a[5],temp;
for(i=0;i<5;i++)
{
printf("\nENTER ELEMENTS of the array:");
scanf("%d",&a[i]);
}
printf("\nTHE ENTERED ARRAY IS:\n");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}
quicksort(a,l,h);
printf("\n After Selection sort the array is:");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}
}
void quicksort(int a[5],int l,int h)
{
int low,high,i,temp,key;
low=l;
high=h;
key=a[((low+high)/2)];
while(low<=high)
{
while(key>a[low])
{
low=low+1;
}
while(key<a[high])
{
high=high-1;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
}
if(l<high)
quicksort(a,l,high);
if(low<h)
quicksort(a,low,h);
}
#include<stdio.h>
void quicksort(int a[],int,int);
void main()
{
int c,l=0,h=4,i,a[5],temp;
for(i=0;i<5;i++)
{
printf("\nENTER ELEMENTS of the array:");
scanf("%d",&a[i]);
}
printf("\nTHE ENTERED ARRAY IS:\n");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}
quicksort(a,l,h);
printf("\n After Selection sort the array is:");
for(i=0;i<5;i++)
{
printf("\n%d",a[i]);
}
}
void quicksort(int a[5],int l,int h)
{
int low,high,i,temp,key;
low=l;
high=h;
key=a[((low+high)/2)];
while(low<=high)
{
while(key>a[low])
{
low=low+1;
}
while(key<a[high])
{
high=high-1;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
}
if(l<high)
quicksort(a,l,high);
if(low<h)
quicksort(a,low,h);
}
OUTPUT |
No comments:
Post a Comment