-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick sort
77 lines (59 loc) · 1.19 KB
/
quick sort
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
72
73
74
75
76
77
//
// main.c
// quick sort
//
// Created by 毛遵杰 on 2019/12/11.
// Copyright © 2019 毛遵杰. All rights reserved.
//
void display(int array[], int maxlen)
{
int i;
for(i = 0; i < maxlen; i++)
{
printf("%-3d", array[i]);
}
printf("\n");
return ;
}
void QuickSort(int *arr, int low, int high)
{
if (low < high)
{
int i = low;
int j = high;
int k = arr[low];
while (i < j)
{
while(i < j && arr[j] >= k)
{
j--;
}
if(i < j)
{
arr[i++] = arr[j];
}
while(i < j && arr[i] < k)
{
i++;
}
if(i < j)
{
arr[j--] = arr[i];
}
}
arr[i] = k;
QuickSort(arr, low, i - 1);
QuickSort(arr, i + 1, high);
}
}
int main()
{
int array[11] = {3,1,4,1,5,9,2,6,5,3,5};
int maxlen = 11;
printf("before sorting\n");
display(array, maxlen);
QuickSort(array, 0, maxlen-1);
printf("after sorting\n");
display(array, maxlen);
return 0;
}