1、思想
每次选出一个基准,然后将数组剩下的元素分为两部分,一部分小于基准放到左边,一部分大于基准放到右边。然后对基准左右两部分分别做同样处理,分治思想。
2、时间复杂度
最快 O(nlogn) ,最坏 O (n^2) ,平均 O(nlogn) 。
3、代码实现
//常规版
public class quickSort {
public static void QuickSort(int[] arr){
QSort(arr, 0, arr.length-1);
}
private static void QSort(int[] arr, int low, int hight) {
int pivot;
if(low < hight){
pivot = Partition(arr, low, hight);
QSort(arr, low, pivot-1);
QSort(arr, pivot+1, hight);
}
}
private static int Partition(int[] arr, int low, int hight) {
if(arr == null || low < 0 || hight >= arr.length){
new Exception();
}
int pivotkey = arr[low];
while(low < hight){
while(low < hight && arr[hight] >= pivotkey) hight--;
Swap(arr, low, hight);
while (low < hight && arr[low] <= pivotkey) low++;
Swap(arr, low, hight);
}
return low;
}
private static void Swap(int[] arr, int low, int hight) {
int temp = arr[low];
arr[low] = arr[hight];
arr[hight] = temp;
}
public static void main(String[] args) {
int[] arr = {50,10,90,30,70,40,80,60,20};
QuickSort(arr);
for (int array : arr) {
System.out.print(array+" ");
}
System.out.println();
}
}
/**
* 三数取中优化版
*/
public class quickSort_up {
public static void QuickSort(int arr[]){
if(arr == null || arr.length == 0) return;
QSort(arr, 0, arr.length-1);
}
private static void QSort(int[] arr, int low, int high) {
int pivot;
if(low < high){
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) {
if(arr == null || low < 0 || high >= arr.length){
new Exception();
}
int pivotkey;
ChoosePivotkey(arr, low, high);
pivotkey = arr[low];
while(low < high){
while(low < high && arr[high] >= pivotkey) high--;
Swap(arr, low, high);
while(low < high && arr[low] <= pivotkey) low++;
Swap(arr, low, high);
}
return low;
}
//三数取中选择枢轴 将枢轴调到第一个位置
private static void ChoosePivotkey(int[] arr, int low, int high) {
int mid = (high + low)/2;
if(arr[low] > arr[high]){
//保证左端较小
Swap(arr, low, high);
}
if(arr[mid] > arr[high]){
//保证中间较小
Swap(arr, low, high);
}
if(arr[mid] > arr[low] ){
Swap(arr, mid, low);
}
}
private static void Swap(int[] arr, int low, int high) {
int temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
}
public static void main(String[] args) {
int[] arr = {50,10,90,30,70,40,80,60,20};
QuickSort(arr);
for (int array : arr) {
System.out.print(array+" ");
}
System.out.println();
}
}
/**
* 此方法为减少无效交换优化法
*/
public class quickSort_up2 {
public static void QuickSort(int[] arr){
if(arr == null || arr.length ==0) return;
QSort(arr, 0, arr.length-1);
}
private static void QSort(int[] arr, int low, int high) {
int pivot;
if(low < high){
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) {
if(arr == null || low <0 || high >= arr.length){
new Exception();
}
int pivotkey = arr[low];
int tempkey = pivotkey;
while(low < high){
while (low < high && arr[high] >= pivotkey) high--;
arr[low] = arr[high];
while(low < high && arr[low] <= pivotkey) low++;
arr[high] = arr[low];
}
arr[low] = tempkey;
return low;
}
public static void main(String[] args) {
int[] arr = {50,10,90,30,70,40,80,60,20};
QuickSort(arr);
for (int array : arr) {
System.out.print(array+" ");
}
System.out.println();
}
}
来源:oschina
链接:https://my.oschina.net/u/3991724/blog/3207354