分治法:用C#实现快速排序
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](/Images/OutliningIndicators/None.gif)
5
namespace QuickSort
6
{
7
class Program
8
{
9
static void Main(string[] args)
10
{
11
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
12
13
QuickSort q = new QuickSort();
14
q.Init(a);
15
q.Sort(0, a.Length - 1);
16![](/Images/OutliningIndicators/InBlock.gif)
17
int[] r = q.GetResult();
18![](/Images/OutliningIndicators/InBlock.gif)
19
if (r != null)
20
{
21
for (int i = 0; i < r.Length; i++)
22
{
23
System.Console.WriteLine(r[i]);
24
}
25
}
26
}
27
}
28![](/Images/OutliningIndicators/InBlock.gif)
29
class QuickSort
30
{
31
private int[] _toBeSort;
32![](/Images/OutliningIndicators/InBlock.gif)
33
private bool _isSort;
34![](/Images/OutliningIndicators/InBlock.gif)
35
public void Init(int[] toBeSort)
36
{
37
_isSort = false;
38
_toBeSort = toBeSort;
39
}
40![](/Images/OutliningIndicators/InBlock.gif)
41
public int[] GetResult()
42
{
43
if (_isSort)
44
{
45
return _toBeSort;
46
}
47
else
48
{
49
return null;
50
}
51
}
52![](/Images/OutliningIndicators/InBlock.gif)
53
public void Sort(int startIndex, int endIndex)
54
{
55
if (_toBeSort == null)
56
{
57
throw new Exception();
58
}
59![](/Images/OutliningIndicators/InBlock.gif)
60
if (endIndex > 1 + startIndex)
61
{
62
int part = partition(startIndex, endIndex);
63![](/Images/OutliningIndicators/InBlock.gif)
64
//_toBeSort[part]已经在合适的位置上了,无需再次考虑。
65
Sort(startIndex, part - 1);
66
Sort(part + 1, endIndex);
67
}
68![](/Images/OutliningIndicators/InBlock.gif)
69
_isSort = true;
70
}
71![](/Images/OutliningIndicators/InBlock.gif)
72
private int partition(int startIndex, int endIndex)
73
{
74
int part = startIndex;
75![](/Images/OutliningIndicators/InBlock.gif)
76
int mid = _toBeSort[startIndex];
77![](/Images/OutliningIndicators/InBlock.gif)
78
int si = startIndex + 1;
79
int bi = endIndex;
80
int temp = 0;
81![](/Images/OutliningIndicators/InBlock.gif)
82
while (si < bi)
83
{
84
while (_toBeSort[si] < mid)
85
{
86
si++;
87
if (si == endIndex)
88
{
89
break;
90
}
91
}
92![](/Images/OutliningIndicators/InBlock.gif)
93
while (_toBeSort[bi] > mid)
94
{
95
bi--;
96
}
97![](/Images/OutliningIndicators/InBlock.gif)
98
if (si < bi)
99
{
100
temp = _toBeSort[bi];
101
_toBeSort[bi] = _toBeSort[si];
102
_toBeSort[si] = temp;
103
si++;
104
bi--;
105
}
106
}
107![](/Images/OutliningIndicators/InBlock.gif)
108
temp = _toBeSort[bi];
109
_toBeSort[bi] = _toBeSort[startIndex];
110
_toBeSort[startIndex] = temp;
111![](/Images/OutliningIndicators/InBlock.gif)
112
part = bi;
113![](/Images/OutliningIndicators/InBlock.gif)
114
return part;
115
}
116
}
117![](/Images/OutliningIndicators/InBlock.gif)
118
119
}
120![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
91
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
106
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
116
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
120
![](/Images/OutliningIndicators/None.gif)