-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.c
71 lines (62 loc) · 1.02 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<stdio.h>
int partition(int *A,int left, int right, int p)
{
int r=left;
int pivot=A[p];
for(int k=left;k<=right;k++)
{
if(pivot>A[k])
r++;
}
A[p]=A[r];
A[r]=pivot;
int i=left;
int j=r+1;
while(i<r && j<right+1)
{
while(i<r&&A[i]<pivot)
{
i++;
}
while(j<right+1&&A[j]>pivot)
{
j++;
}
if(i<left && j<right+1)
{
int temp=A[i];
A[i]=A[j];
A[j]=temp;
i++;
j++;
}
}
return r;
}
void quicksort(int *A,int first,int last)
{
if(first==last)
return;
int pivot=A[first];
int pivot_rank=partition(A,first,last,first)+first;
if(pivot_rank>=first)
{
quicksort(A,first,pivot_rank-1);
}
if(pivot_rank<=last)
{
quicksort(A,pivot_rank+1,last);
}
}
int main()
{
int n;
scanf("%d",&n);
int A[n];
for(int i=0;i<n;i++)
scanf("%d",&A[i]);
quicksort(A,0,n-1);
for(int i=0;i<n;i++)
printf("%d\n",A[i]);
return 0;
}