0%

AlgorithmLearning-D&C

Divide and Conquer分治

快速排序

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
static void QuickSort(int[] data, int left, int right)
{
if(left > right)
return;
int i = left;
int j = right;
int mark = data[left];
while (i != j)
{
while (data[j] >= mark && i < j)
j--;
while (data[i] <= mark && i < j)
i++;
if (i < j)
{
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
data[left] = data[i];
data[i] = mark;
QuickSort(data, left, i - 1);
QuickSort(data, i + 1, right);

}