昨天有人问我快排序,结果居然忘了,惭愧,现在用C#重写一下
class
QuickSort
... {
public static void QSort(int[] A)
...{
Sort(A, 0, A.Length-1);
}
private static void Sort(int[] A, int p, int r)
...{
if (p<r)
...{
int q;
q = Partition(A, p, r);
Sort(A, p, q - 1);
Sort(A, q + 1, r);
}
}
// 分解
private static int Partition(int[] A, int p, int r)
...{
int i = p - 1;
int x = A[r];
for (int j = p; j < r; j++)
...{
if (x > A[j])
...{
i++;
Exchange(ref A[i], ref A[j]);
}
}
Exchange(ref A[i + 1], ref A[r]);
return i + 1;
}
//交换
private static void Exchange(ref int a, ref int b)
...{
int temp;
temp = a;
a = b;
b = temp;
}
}
... {
public static void QSort(int[] A)
...{
Sort(A, 0, A.Length-1);
}
private static void Sort(int[] A, int p, int r)
...{
if (p<r)
...{
int q;
q = Partition(A, p, r);
Sort(A, p, q - 1);
Sort(A, q + 1, r);
}
}
// 分解
private static int Partition(int[] A, int p, int r)
...{
int i = p - 1;
int x = A[r];
for (int j = p; j < r; j++)
...{
if (x > A[j])
...{
i++;
Exchange(ref A[i], ref A[j]);
}
}
Exchange(ref A[i + 1], ref A[r]);
return i + 1;
}
//交换
private static void Exchange(ref int a, ref int b)
...{
int temp;
temp = a;
a = b;
b = temp;
}
}
调试代码
static
void
Main(
string
[] args)
... {
int[] iArrary = new int[] ...{ 98, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
QuickSort.QSort(iArrary);
}
... {
int[] iArrary = new int[] ...{ 98, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
QuickSort.QSort(iArrary);
}