-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick sort.c
52 lines (51 loc) · 1.18 KB
/
Quick sort.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
#include <stdio.h>
//p is the pivot element
//r is the last element
int partition(int* a,int p ,int r) //to find the partition element i.e the element after which every element is greater than itself and before every element is smaller
{
int i=p-1,t,j;
int x = a[r];
for(j=p;j<r;)
{
if(a[j]<=x)
{
i++;
t = a[i];
a[i] = a[j];
a[j] = t;
j++;
}
if(a[j]>x)
{
j++;
}
}
t = a[i+1];
a[i+1] = a[r];
a[r] = t;
return i+1;
}
int* quicksort(int* a,int p,int r)
{
int q;
if(p<r)
{
q = partition(a,p,r); // q is partition element
quicksort(a,p,q-1); //repeating the quick sort on the array before and after partition element
quicksort(a,q+1,r);
}
return a; //now a is sorted array
}
int main()
{
int* p;
int i, size;
scanf("%d", &size); //size of array
int a[size];
for(i=0;i<size;i++) //input array elements
scanf("%d", &a[i]);
p = quicksort(a,0,size-1);
for(i=0;i<size;i++)
printf("%d ", p[i]);
return 0;
}