-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.c
59 lines (54 loc) · 1016 Bytes
/
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
#include <stdio.h>
int Partition(int a[], int low, int high)
{
int pivot = a[low];
int start = low;
int end = high;
while (start < end)
{
while (a[start] <= pivot)
{
start++;
}
while (a[end] > pivot)
{
end--;
}
if (start < end)
{
int tem = a[start];
a[start] = a[end];
a[end] = tem;
}
}
int tem = a[low];
a[low] = a[end];
a[end] = tem;
return end;
}
void Qsort(int a[], int low, int high)
{
if (low < high)
{
int position = Partition(a, low, high);
Qsort(a, low, position - 1); // left subarray
Qsort(a, position + 1, high); // right subarray
}
return;
}
int main()
{
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
Qsort(a, 0, n - 1);
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}