-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.c
61 lines (55 loc) · 870 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
#include<stdio.h>
#include"abc.h"
void qsort(int a[],int l,int r);
int part(int a[],int l,int r);
void swap(int *a,int *b);
void main()
{
int n,a[50];
printf("enter the number of elements");
scanf("%d",&n);
read(a,n);
printf("\narry before sorting\n");
print(a,n);
qsort(a,0,n-1);
printf("\narry after sorting\n");
print(a,n);
}
void qsort(int a[],int l,int r)
{
int pos;
if(l<r)
{
pos = part(a,l,r);
qsort(a,l,pos-1);
qsort(a,pos+1,r);
}
}
int part(int a[],int l,int r)
{
int key = a[l],i=l+1,j=r;
do
{
while(a[i]<key && i<r)
{
i++;
}
while(a[j]>key && j>l)
{
j--;
}
if(i<j)
{
swap(&a[i],&a[j]);
}
}while(i<j);
swap(&a[l],&a[j]);
return j;
}
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}