Data structure using C
Write C program for Quick sort.
#include<stdio.h>
#include<conio.h>
int split(int lp,int up);
void quick(int lp,int up);
int a[20];
void main()
{
int n,i,lp,up;
clrscr();
printf("\n Enter how many number u want:-");
scanf(" %d",&n);
printf("\n Enter %d numbers:-",n);
for(i=0;i<n;i++)
{
scanf(" %d",&a[i]);
}
printf("\n Before sorting numbers are:-");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
quick(0,n-1);
printf("\n After sorting numbers are:-");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
void quick(int lp,int up)
{
int i;
if(up>lp)
{
i=split(lp,up);
quick(lp,i-1);
quick(i+1,up);
}
}
int split(int lp,int up)
{
int k,p,q,t;
p=lp+1;
q=up;
k=a[lp];
while(q>=p)
{
while(a[p]<k)
{
p++;
}
while(a[q]>k)
{
q--;
}
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lp];
a[lp]=a[q];
a[q]=t;
return(q);
}
/*
Enter how many number u want:-5
Enter 5 numbers:-2
6
1
0
9
Before sorting numbers are:-
2
6
1
0
9
After sorting numbers are:-
0
1
2
6
9 */
Write C program for Quick sort.
#include<stdio.h>
#include<conio.h>
int split(int lp,int up);
void quick(int lp,int up);
int a[20];
void main()
{
int n,i,lp,up;
clrscr();
printf("\n Enter how many number u want:-");
scanf(" %d",&n);
printf("\n Enter %d numbers:-",n);
for(i=0;i<n;i++)
{
scanf(" %d",&a[i]);
}
printf("\n Before sorting numbers are:-");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
quick(0,n-1);
printf("\n After sorting numbers are:-");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();
}
void quick(int lp,int up)
{
int i;
if(up>lp)
{
i=split(lp,up);
quick(lp,i-1);
quick(i+1,up);
}
}
int split(int lp,int up)
{
int k,p,q,t;
p=lp+1;
q=up;
k=a[lp];
while(q>=p)
{
while(a[p]<k)
{
p++;
}
while(a[q]>k)
{
q--;
}
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lp];
a[lp]=a[q];
a[q]=t;
return(q);
}
/*
Enter how many number u want:-5
Enter 5 numbers:-2
6
1
0
9
Before sorting numbers are:-
2
6
1
0
9
After sorting numbers are:-
0
1
2
6
9 */