快速排序由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
public class quicksoter
{
public void Sort(int[] list, int s, int t)
{
int low, high, key, j;
low = s;
high = t;
if (s < t)
{
key = list[s];
while (low < high)
{
while (low < high && list[high] >= key)
high--;
list[low] = list[high];
while (low < high && list[low] <= key)
low++;
list[high] = list[low];
}
list[low] = key;
j = low;
Sort(list, s, j - 1);
Sort(list, j + 1, t);
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArray = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
for (int i = 0; i < iArray.Length; i++)
Console.Write("{0} ", iArray[i]);
Console.WriteLine();
quicksoter bb = new quicksoter();
bb.Sort(iArray, 0, iArray.Length - 1);
for (int i = 0; i < iArray.Length; i++)
Console.Write("{0} ", iArray[i]);
Console.WriteLine();
Console.ReadKey();
}
}