代码如下
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QuickSorter
{
class Program
{
static void swap(ref int a, ref int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
static void quickSort(int[] arr, int left, int right)
{
int i, j, s;
if (left < right)
{
i = left - 1;
j = right + 1;
s = arr[(i + j) / 2];//划分基准线(i + j) / 2的取值
while (true)
{
while (arr[++i] < s) ;
while (arr[--j] > s) ;
if (i >= j)
break;
swap(ref arr[i], ref arr[j]);
}
quickSort(arr, left, i - 1);//对左区间递归排序
quickSort(arr, j + 1, right);//对右区间递归排序
}
}
static void Main(string[] args)
{
int[] arr = { 6, 1, 5, 3, 7, 11, 35, 6, 12, 82, 12, 39, 28, 39, 50 };
// int[] arr = { 6, 1, 5, 3, 7, 11, 35, 6, 12, 82, 12, 39, 28, 39, 50 };
// int[] arr = { 6, 1, 5, 3, 6, 10, 55, 9, 12, 87, 12, 34, 75, 33, 47 };
quickSort(arr, 0, arr.Length - 1);
Console.WriteLine("快速排序结果");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
}