/*write a c program to sort array element using Quick
sort mothod*/
#include<stdio.h>
#include<conio.h>
int split(int *,int,int);
void main()
{
int a[20],i,n;
void sort(int *,int,int);
clrscr();
printf("\nenter how many number:-");
scanf("%d",&n);
printf("enter the element for an array:-\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,0,n-1);
printf("\narray after sorting:-");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void sort(int z[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(z,lower,upper);
sort(z,lower,i-1);
sort(z,i+1,upper);
}
}
int split(int z[],int lower, int upper)
{
int i,a,b,t;
a=lower+1;
b=upper;
i=z[lower];
while(b>=a)
{
while(z[a]<i)
a++;
while(z[b]>i)
b--;
if(b>a)
{
t=z[a];
z[a]=z[b];
z[b]=t;
}
}
t=z[lower];
z[lower]=z[b];
z[b]=t;
return b;
}
/*
enter how many number:-7
enter the element for an array:-
4
7
8
9
6
3
2
array after sorting:-
2
3
4
6
7
8
9
*/
No comments:
Post a Comment