-
Notifications
You must be signed in to change notification settings - Fork 0
/
qs.c
60 lines (57 loc) · 906 Bytes
/
qs.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
#include<stdio.h>
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
int partition(int a[],int i,int j)
{
int t;
t=i;
while(i<j)
{
while(a[i]<=a[t])
{
i++;
}
while(a[j]>a[t])
{
j--;
}
if(i<j)
{
swap(a,i,j);
}
}
swap(a,t,j);
return j;
}
void qs(int a[],int l,int r)
{
int j;
if(l<r)
{
j=partition(a,l,r);
qs(a,l,j-1);
qs(a,j+1,r);
}
}
void main()
{
int i,n,a[25];
printf("enter limit");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
qs(a,0,n-1);
printf("sorted array is \n");
for ( i = 0; i < n; i++)
{
printf("\t%d",a[i]);
}
}