-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick.c
55 lines (43 loc) · 764 Bytes
/
quick.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
#include<stdio.h>
int a[100];
void swap(int *p,int *q)
{
int t=*p; *p=*q; *q=t;
}
void split(int low,int high,int *partition)
{
int pivot,i,j;
pivot = a[low] ;
i = low ;
j = high ;
while (i<j )
{
while ( a[i] <= pivot && i < j )
i++;
while ( a[j] > pivot )
j--;
if ( i < j )
swap(&a[i],&a[j]);
}
*partition = j ;
swap(&a[low],&a[j]);
}
void quick(int low,int high)
{
int partition;
if (low<high )
{ split (low,high,&partition) ;
quick (low,partition-1) ;
quick (partition+1,high) ;
}
}
main()
{
int n,i;
printf("Enter N "); scanf("%d",&n);
printf("Enter data\n");
for(i=0;i<n;i++) scanf("%d",&a[i]);
quick(0,n-1);
printf("After sorting \n");
for(i=0;i<n;i++) printf("%d\n",a[i]);
}