快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)。
该方法的基本思想是:
1.先从数列中取出一个数作为基准数。
2.分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。
3.再对左右区间重复第二步,直到各区间只有一个数。
之前提到过,Array的Sort()方法采用的是快速排序方法,通过使用.NET Reflector对程序集mscorlib进行解析,分析源码。
首先安装.NET Reflector,打开VS2010,新建一个项目,键入如下代码:
string [] names = {"wang", "li", "liu", "zhao"};
Array.Sort(names);
在Sort上右键,选择“Open in .NET Reflector Desktop”,便会自动打开.NET Reflector:
public static void Sort(T[] array)
{ if (array == null)
{
throw new ArgumentNullException("array");
}
Sort(array, array.GetLowerBound(0), array.Length, null);
}
点击Sort(array, array.GetLowerBound(0), array.Length, null):
public static void Sort(T[] array, int index, int length, IComparer comparer)
{ if (array == null)
{ throw new ArgumentNullException("array");
}
if ((index < 0) || (length < 0))
{ throw new ArgumentOutOfRangeException((length < 0) ? "length" : "index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((array.Length - index) < length)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
if ((length > 1) && (((comparer != null) && (comparer != Comparer.Default)) || !TrySZSort(array, null, index, (index + length) - 1)))
{
ArraySortHelper.Default.Sort(array, index, length, comparer);
}
}
点击 ArraySortHelper.Default.Sort(array, index, length, comparer):
internal class ArraySortHelper : IArraySortHelper
{
// Fields
private static IArraySortHelper defaultArraySortHelper;
// Methods
public ArraySortHelper();
public int BinarySearch(T[] array, int index, int length, T value, IComparer comparer);
[SecuritySafeCritical]
private static IArraySortHelper CreateArraySortHelper();
internal static int InternalBinarySearch(T[] array, int index, int length, T value, IComparer comparer);
internal static void QuickSort(T[] keys, int left, int right, IComparer comparer);
public void Sort(T[] keys, int index, int length, IComparer comparer);
private static void SwapIfGreaterWithItems(T[] keys, IComparer comparer, int a, int b);
// Properties
public static IArraySortHelper Default { get; }
}
此时,可以看到快速排序,点击QuickSort,得到快速排序的源码:
internal static void QuickSort(T[] keys, int left, int right, IComparer comparer)
{
do
{
int a = left;
int b = right;
int num3 = a + ((b - a) >> 1);
ArraySortHelper.SwapIfGreaterWithItems(keys, comparer, a, num3);
ArraySortHelper.SwapIfGreaterWithItems(keys, comparer, a, b);
ArraySortHelper.SwapIfGreaterWithItems(keys, comparer, num3, b);
T y = keys[num3];
do
{
while (comparer.Compare(keys[a], y) < 0)
{
a++;
}
while (comparer.Compare(y, keys[b]) < 0)
{
b--;
}
if (a > b)
{
break;
}
if (a < b)
{
T local2 = keys[a];
keys[a] = keys[b];
keys[b] = local2;
}
a++;
b--;
}
while (a <= b);
if ((b - left) <= (right - a))
{
if (left < b)
{
ArraySortHelper.QuickSort(keys, left, b, comparer);
}
left = a;
}
else
{
if (a < right)
{
ArraySortHelper.QuickSort(keys, a, right, comparer);
}
right = b;
}
}
while (left < right);
}
internal static void QuickSort(T[] keys, int left, int right, IComparer comparer)
{
do
{
int a = left;
int b = right;
int num3 = a + ((b - a) >> 1);
ArraySortHelper.SwapIfGreaterWithItems(keys, comparer, a, num3);
ArraySortHelper.SwapIfGreaterWithItems(keys, comparer, a, b);
ArraySortHelper.SwapIfGreaterWithItems(keys, comparer, num3, b);
T y = keys[num3];
do
{
while (comparer.Compare(keys[a], y) < 0)
{
a++;
}
while (comparer.Compare(y, keys[b]) < 0)
{
b--;
}
if (a > b)
{
break;
}
if (a < b)
{
T local2 = keys[a];
keys[a] = keys[b];
keys[b] = local2;
}
a++;
b--;
}
while (a <= b);
if ((b - left) <= (right - a))
{
if (left < b)
{
ArraySortHelper.QuickSort(keys, left, b, comparer);
}
left = a;
}
else
{
if (a < right)
{
ArraySortHelper.QuickSort(keys, a, right, comparer);
}
right = b;
}
}
while (left < right);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。