Practical # 5
/*Write a program to sort an array using the quick sort */
#include<stdio.h>
#include<conio.h>
void quick_sort(int a[],int first,int last);
void main()
{
int a[10],n,i;
clrscr();
printf("Enter total no's of element=");
scanf("%d",&n);
if(n>10)
{
printf("OVERFLOW");
}
else
{
printf("The unsorted elements are=");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
quick_sort(a,0,n-1);
printf("The sorted elements are=");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
void quick_sort(int a[],int first,int last)
{
int i,j,k,t,temp;
if(first<last)
{
i=first;
j=last+1;
k=first;
do
{
do
{
i++;
}
while(a[i]<a[k]);
do
{
j--;
}
while(a[j]>a[k]);
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}while(i<j);
temp=a[k];
a[k]=a[j];
a[j]=temp;
quick_sort(a,first,j-1);
quick_sort(a,j+1,last);
}
}
Output:-
Enter total no's of element=5
The unsorted elements are=5
4
3
2
1
The sorted elements are=12345
No comments :
Post a Comment