-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
namespace CSharpLearning
-
{
-
using System;
-
-
-
-
-
public static class Program
-
{
-
-
-
-
public static void Main()
-
{
-
int[] a = { 101, 93, 856, 7, 62, 15, 84, 3, 298, 1256 };
-
Console.WriteLine("Before Quick Sort:");
-
foreach (int i in a)
-
{
-
Console.Write(i + " ");
-
}
-
-
Console.WriteLine("\r\n");
-
Console.WriteLine("In Quick Sort:");
-
QuickSort(a, 0, 9);
-
Console.WriteLine("\r\nAfter Quick Sort:");
-
foreach (int i in a)
-
{
-
Console.Write(i + " ");
-
}
-
-
Console.WriteLine(string.Empty);
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private static void QuickSort(int[] a, int low, int high)
-
{
-
if (low >= high)
-
{
-
return;
-
}
-
-
int pivot = QuickSortOnce(a, low, high);
-
-
-
foreach (int i in a)
-
{
-
Console.Write(i + " ");
-
}
-
-
Console.WriteLine(string.Empty);
-
-
-
QuickSort(a, low, pivot - 1);
-
-
-
QuickSort(a, pivot + 1, high);
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private static int QuickSortOnce(int[] a, int low, int high)
-
{
-
-
int pivot = a[low];
-
int i = low, j = high;
-
-
while (i < j)
-
{
-
-
while (a[j] >= pivot && i < j)
-
{
-
j--;
-
}
-
-
-
a[i] = a[j];
-
-
-
while (a[i] <= pivot && i < j)
-
{
-
i++;
-
}
-
-
-
a[j] = a[i];
-
}
-
-
-
a[i] = pivot;
-
return i;
-
}
-
}
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
算法 quick sort
原文:http://www.cnblogs.com/wzjhoutai/p/7294288.html