/*QUICK SORT:-C Program created by Prashant Shinde*/
#include<stdio.h>
#include<conio.h>
int A[10];
void qsort(int,int);
int partition(int,int);
void input(int);
void display(int);
void main()
{
int n;
clrscr();
printf("Enter the no. of element=");
scanf("%d",&n);
input(n);
qsort(0,n-1);
printf("\nThe sorrted list is:-\n");
display(n);
getch();
}
void input(int n)
{
int i;
for (i=0;i<n;i++)
{
printf("enter a value=");
scanf("%d",&A[i]);
}
}
void display(int n)
{
int i;
for (i=0;i<n;i++)
{
printf("A[%d]=%d\n",i,A[i]);
}
}
void qsort(int L,int H)
{
int m;
if(L<H)
{
m=partition(L,H);
qsort(L,m-1);
qsort(m+1,H);
}
}
int partition(int L,int H)
{
int i=L,j=H,pivote=A[L],t;
while(i<j)
{
while((A[i]<=pivote)&&(i<=H))
{
i++;
}
while((A[j]>pivote)&&(j>=L))
{
j--;
}
if(i<j)
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
t=A[L];
A[L]=A[j];
A[j]=t;
return j;
}
OUTPUT:-
Enter the no. of element=7
enter a value=56
enter a value=89
enter a value=23
enter a value=14
enter a value=100
enter a value=159
enter a value=0
The sorrted list is:-
A[0]=0
A[1]=14
A[2]=23
A[3]=56
A[4]=89
A[5]=100
A[6]=159
#include<stdio.h>
#include<conio.h>
int A[10];
void qsort(int,int);
int partition(int,int);
void input(int);
void display(int);
void main()
{
int n;
clrscr();
printf("Enter the no. of element=");
scanf("%d",&n);
input(n);
qsort(0,n-1);
printf("\nThe sorrted list is:-\n");
display(n);
getch();
}
void input(int n)
{
int i;
for (i=0;i<n;i++)
{
printf("enter a value=");
scanf("%d",&A[i]);
}
}
void display(int n)
{
int i;
for (i=0;i<n;i++)
{
printf("A[%d]=%d\n",i,A[i]);
}
}
void qsort(int L,int H)
{
int m;
if(L<H)
{
m=partition(L,H);
qsort(L,m-1);
qsort(m+1,H);
}
}
int partition(int L,int H)
{
int i=L,j=H,pivote=A[L],t;
while(i<j)
{
while((A[i]<=pivote)&&(i<=H))
{
i++;
}
while((A[j]>pivote)&&(j>=L))
{
j--;
}
if(i<j)
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
}
t=A[L];
A[L]=A[j];
A[j]=t;
return j;
}
OUTPUT:-
Enter the no. of element=7
enter a value=56
enter a value=89
enter a value=23
enter a value=14
enter a value=100
enter a value=159
enter a value=0
The sorrted list is:-
A[0]=0
A[1]=14
A[2]=23
A[3]=56
A[4]=89
A[5]=100
A[6]=159
No comments:
Post a Comment