#include<stdio.h>
#include<conio.h>
void quicksort(int a[20],int p,int r);
int partition(int a[20],int p,int r);
int main()
{
int a[20],n,op,p,r,at;
printf("N : ");
scanf("%d",&n);
printf("\nEnter the number : ");
for(op=1;op<=n;op++)
scanf("%d",&a[op]); // Enter the number.
p=1;
r=n;
quicksort(a,p,r);
printf("\nThe Sorted number : ");
for(at=1;at<=n;at++)
printf("%d ",a[at]);
getch();
return 0;
}
void quicksort(int a[20],int p,int r)
{ int q;
if(p<r)
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
int partition(int a[20],int p,int r)
{
int x,i,j,temp,count;
x=a[r];
i=p-1;
for(j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
count=a[i+1];
a[i+1]=a[r];
a[r]=count;
return i+1;
}
#include<conio.h>
void quicksort(int a[20],int p,int r);
int partition(int a[20],int p,int r);
int main()
{
int a[20],n,op,p,r,at;
printf("N : ");
scanf("%d",&n);
printf("\nEnter the number : ");
for(op=1;op<=n;op++)
scanf("%d",&a[op]); // Enter the number.
p=1;
r=n;
quicksort(a,p,r);
printf("\nThe Sorted number : ");
for(at=1;at<=n;at++)
printf("%d ",a[at]);
getch();
return 0;
}
void quicksort(int a[20],int p,int r)
{ int q;
if(p<r)
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
int partition(int a[20],int p,int r)
{
int x,i,j,temp,count;
x=a[r];
i=p-1;
for(j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
count=a[i+1];
a[i+1]=a[r];
a[r]=count;
return i+1;
}
No comments:
Post a Comment