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
using namespace std;
void (int *sortList, int number)
{
int i = 0;
for (; i <= number; i++)
{
cout<<sortList[i]<<" ";
}
cout<<endl;
}
static int improveQsort(int *sortList, int firstNum, int lastNum)
{
int tmpNum;
tmpNum = sortList[firstNum];
while ( firstNum < lastNum )
{
while ( firstNum < lastNum && sortList[lastNum] > tmpNum )
{
lastNum--;
}
if ( firstNum < lastNum )
{
sortList[firstNum++] = sortList[lastNum];
}
while ( firstNum < lastNum && sortList[firstNum] <= tmpNum )
{
firstNum++;
}
if ( firstNum < lastNum )
{
sortList[lastNum--] = sortList[firstNum];
}
}
sortList[firstNum] = tmpNum;
return firstNum;
}
static void quickSort(int *sortList, int firstNum, int lastNum)
{
int current = 0;
if ( firstNum < lastNum ){
current = improveQsort(sortList, firstNum, lastNum);
quickSort(sortList, firstNum, current-1);
quickSort(sortList,current+1,lastNum);
}
}
int main (int argc, char *argv[])
{
int sortList[10] = {5, 3, 7, 4, 1, 9, 8, 6, 2, 3};
int number = 10;
quickSort(sortList,0,number-1);
showResult(sortList,number-1);
return 0;
}