class
QuickSort
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public void Sort(int[] data, int start, int end)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (start >= end) return;
if (start + 1 == end)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (data[start] > data[end])
Swap(data, start, end);
![](/Images/OutliningIndicators/InBlock.gif)
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
int indexL = start + 1, indexR = end;
while (indexL < indexR)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Get from left
while (indexL <= end && data[start] >= data[indexL])
indexL++;
![](/Images/OutliningIndicators/InBlock.gif)
// Get from right
while (indexR > start && data[start] < data[indexR])
indexR--;
![](/Images/OutliningIndicators/InBlock.gif)
if (indexL < indexR)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Swap(data, indexR, indexL);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
if(indexL-1 !=start)
Swap(data, start, indexL - 1);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Sort(data, start, indexL - 2);
Sort(data, indexL, end);
}
![](/Images/OutliningIndicators/InBlock.gif)
private void Swap(int[] data, int x, int y)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
data[x] = data[x] + data[y];
data[y] = data[x] - data[y];
data[x] = data[x] - data[y];
}
}
转载于:https://www.cnblogs.com/skywind/archive/2008/03/31/1131612.html