http://flyingcat2013.blog.51cto.com/7061638/1281614
一 这是第一种比较简单的写法:
public static void quickSort(int[] arr){
qsort(arr, 0, arr.length-1);
}
private static void qsort(int[] arr, int low, int high){
if (low < high){
int pivot=partition(arr, low, high); //将数组分为两部分
qsort(arr, low, pivot-1); //递归排序左子数组
qsort(arr, pivot+1, high); //递归排序右子数组
}
}
private static int partition(int[] arr, int low, int high){
int pivot = arr[low]; //枢轴记录
while (low<high){
while (low<high && arr[high]>=pivot) --high;
arr[low]=arr[high]; //交换比枢轴小的记录到左端
while (low<high && arr[low]<=pivot) ++low;
arr[high] = arr[low]; //交换比枢轴小的记录到右端
}
//扫描完成,枢轴到位
arr[low] = pivot;
//返回的是枢轴的位置
return low;
}
二这是比较好理解的写法
static void Main(string[] args)
{
int[] a = { 1, 4, 67, 34, 7, 2, 454,8,56,23,343,435,654,1231,45,6652,34,234,456,534, 87 };
Sort(a, 0, a.Length - 1);
for(int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.ReadLine();
}
public static int partition(int[] array, int low, int hight)
{
int pivotVakue = low;
while (low < hight)
{
while (low < hight && array[hight] >= array[pivotVakue])
{
hight--;
}
while (low < hight && array[low] < array[pivotVakue])
{
low++;
}
//完成low和hight值的交换
int temo = array[low];
array[low] = array[hight];
array[hight] = temo;
}//完成枢轴的交换
int temp = array[pivotVakue];
array[pivotVakue] = array[low] ;
array[low] = temp;
return low;
}
public static void Sort(int[] array, int low, int hight)
{
if (low < hight)
{
int pivot = partition(array, low, hight);
Sort(array, low, pivot - 1);
Sort(array, pivot + 1, hight);
}
}
}
一 这是第一种比较简单的写法:
public static void quickSort(int[] arr){
qsort(arr, 0, arr.length-1);
}
private static void qsort(int[] arr, int low, int high){
if (low < high){
int pivot=partition(arr, low, high); //将数组分为两部分
qsort(arr, low, pivot-1); //递归排序左子数组
qsort(arr, pivot+1, high); //递归排序右子数组
}
}
private static int partition(int[] arr, int low, int high){
int pivot = arr[low]; //枢轴记录
while (low<high){
while (low<high && arr[high]>=pivot) --high;
arr[low]=arr[high]; //交换比枢轴小的记录到左端
while (low<high && arr[low]<=pivot) ++low;
arr[high] = arr[low]; //交换比枢轴小的记录到右端
}
//扫描完成,枢轴到位
arr[low] = pivot;
//返回的是枢轴的位置
return low;
}
二这是比较好理解的写法
static void Main(string[] args)
{
int[] a = { 1, 4, 67, 34, 7, 2, 454,8,56,23,343,435,654,1231,45,6652,34,234,456,534, 87 };
Sort(a, 0, a.Length - 1);
for(int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
Console.ReadLine();
}
public static int partition(int[] array, int low, int hight)
{
int pivotVakue = low;
while (low < hight)
{
while (low < hight && array[hight] >= array[pivotVakue])
{
hight--;
}
while (low < hight && array[low] < array[pivotVakue])
{
low++;
}
//完成low和hight值的交换
int temo = array[low];
array[low] = array[hight];
array[hight] = temo;
}//完成枢轴的交换
int temp = array[pivotVakue];
array[pivotVakue] = array[low] ;
array[low] = temp;
return low;
}
public static void Sort(int[] array, int low, int hight)
{
if (low < hight)
{
int pivot = partition(array, low, hight);
Sort(array, low, pivot - 1);
Sort(array, pivot + 1, hight);
}
}
}