-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.cpp
42 lines (40 loc) · 972 Bytes
/
quickSort.cpp
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
#include "main.h"
//快速排序的具体实现
int sortCount = 0;
//递归部分
void doAction(int tempData[], int low, int high)
{
int i = low;
int j = high;
if (i > j)
return;
int temp = tempData[low];
while (i != j)
{
//尾部与基准比较
while (tempData[j] >= temp && i < j)
j--;
//头部与基准比较
while (tempData[i] <= temp && i < j)
i++;
if (i < j)
swap(tempData[i], tempData[j]);
}
//将基准temp放于自己的位置,(第i个位置)
swap(tempData[low], tempData[i]);
printCurrentResult(tempData, sortCount++);
doAction(tempData, low, i - 1);
doAction(tempData, i + 1, high);
}
void quickSort(int testData[])
{
int tempData[testDataCount];
//转存到临时数组
for (int i = 0; i < testDataCount; i++)
{
tempData[i] = testData[i];
}
sortCount = 0;
doAction(tempData, 0, testDataCount-1);
printCurrentResult(tempData, -1);
}