
Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Sort


{
class Program

{
static void Main(string[] args)

{

int[] A = new int[]
{49,1,38,65,97,176,13,27,15,33,100,20,1,0,100,0,0,33,100,20,1};
// int[] A = new int[] {4,3,2,1 };
QuickSort(A, 0, A.Length - 1);

}
public static void QuickSort(int[] A, int begin, int end)

{
if (begin < end)

{

int q = Partition(A, begin, end);

PrintArray(A, A.Length); //输出经过一趟快速排序后的结果

QuickSort(A, begin, q - 1);

QuickSort(A, q+1, end);
}
}

public static int Partition(int[] A, int begin, int end)

{
int low, high;
low = begin;
high = end;
int x = A[low];
int xi=low;
int t;
while (low < high)

{

//从数值末向后搜索找到第一个比x小的数与x交换
while (high >= 0 && A[high] >=x && low <= high)

{
high--;
}

if (high >= 0)

{
t = A[high];
A[high] = x;
A[xi] = t;
xi = high;

}

//从数组头开始找到第一个比x大的数与x交换
while (low <= end && A[low] <x && low <= high)

{
low++;
}

if (low < end)

{

t = A[low];
A[low] = x;
A[xi] = t;
xi = low;


}

//if (low < high)
//{
// t = A[low];
// A[low] = A[high];
// A[high]=t;
//}
}

return low;

}

public static void PrintArray(int[] A, int length)

{
int i;
for (i = 0; i < length; i++)

{
Console.Write(A[i]);
Console.Write(",");
}

Console.Write("\n");
}

}
}

转载于:https://www.cnblogs.com/smile2you/archive/2009/05/06/1450958.html