-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicksort.c
65 lines (52 loc) · 1017 Bytes
/
quicksort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdio.h>
int partition( int x[], int lb, int ub)
{
int up,down,a,temp;
a=x[lb];
up=ub;
down=lb;
while(down <up)
{
while(x[down]<=a && down<ub)
down++;
while(x[up]>a)
up--;
if(down<up)
{
temp=x[down];
x[down]=x[up];
x[up]=temp;
}
}
x[lb]=x[up];
x[up]=a;
return up;
}
void quick(int x[],int lb, int ub)
{
int pivot;
if(lb>=ub)
return;
pivot=partition(x,lb,ub);
quick(x,lb,pivot-1);
quick(x,pivot+1,ub);
}
void main()
{
int i,n;
int a[100];
printf("\n\nHOW MANY NUMBERS ARE THERE : \t");
scanf("%d",&n);
printf("\n\nENTER NUMBERS ONE BY ONE ....");
for(i=0;i<n;i++)
{
printf("\nENTER NUMBER %d : ",i+1);
scanf("%d",&a[i]);
}
quick(a,0,n);
printf("\n\nSORTED USING QUICK SORT ...\n\n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}