forked from EdisonTalk/DesignPattern.Samples.CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSortHelper.cs
56 lines (50 loc) · 1.22 KB
/
QuickSortHelper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EDC.DesignPattern.Adapter
{
/// <summary>
/// 适配者A:快速排序类
/// </summary>
public class QuickSortHelper
{
public int[] QuickSort(int[] array)
{
Sort(array, 0, array.Length - 1);
return array;
}
public void Sort(int[] array, int p, int r)
{
int q = 0;
if (p < r)
{
q = Partition(array, p, r);
Sort(array, p, q - 1);
Sort(array, q + 1, r);
}
}
public int Partition(int[] array, int p, int r)
{
int x = array[r];
int j = p - 1;
for (int i = p; i <= r - 1; i++)
{
if (array[i] <= x)
{
j++;
Swap(array, j, i);
}
}
Swap(array,j+1,r);
return j + 1;
}
public void Swap(int[] array, int i, int j)
{
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}