-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.C
53 lines (48 loc) · 850 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
// QuickSort //
#include<stdio.h>
int partition(int temp[], int l, int r)
{
int i, pivot=temp[r], pindex=l, flag;
for(i=l;i<r;i=i+1)
{
if(temp[i]<=pivot)
{
flag=temp[i];
temp[i]=temp[pindex];
temp[pindex]=flag;
pindex=pindex+1;
}
}
flag=temp[r];
temp[r]=temp[pindex];
temp[pindex]=flag;
return(pindex);
}
void quicksort(int temp[], int l, int r)
{
int ind;
if(l>=r)
{
return;
}
else
{
ind=partition(temp, l, r);
quicksort(temp, l, ind-1);
quicksort(temp, ind+1, r);
}
}
int main()
{
int n, A[1000], i;
scanf("%d", &n);
for(i=0;i<n;i=i+1)
{
scanf("%d", &A[i]);
}
quicksort(A, 0, n-1);
for(i=0;i<n;i=i+1)
{
printf("%d ", A[i]);
}
}