![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Sort
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
public static void QuickSort(int[] A, int begin, int end)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (begin < end)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int q = Partition(A, begin, end);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
PrintArray(A, A.Length); //输出经过一趟快速排序后的结果
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
QuickSort(A, begin, q - 1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
QuickSort(A, q+1, end);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static int Partition(int[] A, int begin, int end)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int low, high;
low = begin;
high = end;
int x = A[low];
int xi=low;
int t;
while (low < high)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从数值末向后搜索找到第一个比x小的数与x交换
while (high >= 0 && A[high] >=x && low <= high)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
high--;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (high >= 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
t = A[high];
A[high] = x;
A[xi] = t;
xi = high;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从数组头开始找到第一个比x大的数与x交换
while (low <= end && A[low] <x && low <= high)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
low++;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (low < end)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
t = A[low];
A[low] = x;
A[xi] = t;
xi = low;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//if (low < high)
//{
// t = A[low];
// A[low] = A[high];
// A[high]=t;
//}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return low;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void PrintArray(int[] A, int length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i;
for (i = 0; i < length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.Write(A[i]);
Console.Write(",");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Write("\n");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted @
2009-05-06 20:26
smile2you
阅读(
739)
评论()
编辑
收藏
举报