代码
void main()
{
//Init array
int[] a = new int[10];
a[0] = 10;
a[1] = 1;
a[2] = 3;
a[3] = 2;
a[4] = 6;
a[5] = 5;
a[6] = 4;
a[7] = 9;
a[8] = 8;
a[9] = 7;
// Sort
QuickSort(a, 0, 9);
string s = "";
for (int i = 0; i < 10; i++)
{
s += a[i].ToString();
}
txtResult.Text = s;
}
void QuickSort(int[] array, int leftBound, int rightBound)
{
if (leftBound < rightBound)
{
int middle = array[(leftBound + rightBound) / 2];
int i = leftBound-1;
int j = rightBound+1;
while (true)
{
while (array[++i] > middle) ;
while (array[--j] < middle) ;
if (i >= j)
break;
Swap(array, i, j);
}
QuickSort(array, leftBound, i-1);
QuickSort(array, j+1, rightBound);
}
}
void Swap(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
{
//Init array
int[] a = new int[10];
a[0] = 10;
a[1] = 1;
a[2] = 3;
a[3] = 2;
a[4] = 6;
a[5] = 5;
a[6] = 4;
a[7] = 9;
a[8] = 8;
a[9] = 7;
// Sort
QuickSort(a, 0, 9);
string s = "";
for (int i = 0; i < 10; i++)
{
s += a[i].ToString();
}
txtResult.Text = s;
}
void QuickSort(int[] array, int leftBound, int rightBound)
{
if (leftBound < rightBound)
{
int middle = array[(leftBound + rightBound) / 2];
int i = leftBound-1;
int j = rightBound+1;
while (true)
{
while (array[++i] > middle) ;
while (array[--j] < middle) ;
if (i >= j)
break;
Swap(array, i, j);
}
QuickSort(array, leftBound, i-1);
QuickSort(array, j+1, rightBound);
}
}
void Swap(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}