//快速排序的具體過程如下:
//1:在待排序的n個記錄中,任取一個作爲基準,把數組分爲兩部分,第一組的排序碼都小於或等於該排序碼,第二組的排序碼,都大於或等於該排序碼,並把該基準放在兩組中間。
2:採用同樣的方法分別對左右兩組進行排序,直到所有的記錄都排到適當的位置。
///快速遞歸排序
private static void QuickSort(int[] R, int low, int high)
{
int pivotLoc = 0;
if (low < high)
{
pivotLoc = Partition(R, low, high);
QuickSort(R, low, pivotLoc - 1);
QuickSort(R, pivotLoc + 1, high);
}
}
private static int Partition(int[] R, int low, int high)
{
int temp = R[low];
while (low < high)
{
if (low < high && temp <= R[high])
{
high--;
}
R[low] = R[high];
if (low < high && R[low] <= temp)
{
low++;
}
R[high] = R[low];
}
R[low] = temp;
return low;
}
//快速非遞歸排序
public static void QuickSort(int [] R,int Low,int High,Stack<int> stack)
{
int low = Low;
int high = High;
int temp = R[low];
while (high > low)
{
while (low < high && temp <= R[high])
{
high--;
}
if (high > low)
{
R[low] = R[high];
R[high] = temp;
}
while (low < high && temp >= R[low])
{
low++;
}
if (high > low)
{
R[high] = R[low];
R[low] = temp;
}
if (Low < low - 1)
{
stack.Push(Low);
stack.Push(low-1);
}
if (High > low + 1)
{
stack.Push(low+1);
stack.Push(High);
}
}
}