快速排序

拜拜、爱过 提交于 2020-01-28 01:36:56

快速排序

时间复杂度: O(N * log2 N)
最坏情况下: O (N ^ 2)
空间复杂度: O(log2 N)

代码示例:

(1)递归实现快速排序
public static void quickSort(int[] array){
    quick(array,0,array.length - 1);
}
public static void quick(int[] array,int low,int high){
    int par = partion(array,low,high);
    if(par > low + 1){
         quick(array,low,par - 1);
    }
    if(par < high - 1){
         quick(array,par + 1,high);
    }
}
//找基准
public static int partion(int[] array,int low,int high){
    int tmp = array[low];
    while(low < high){
        while(low < high && array[high] >= tmp){
            high--;
        }
        if(low >= high){
             array[low] = tmp;
             break;
        }else {
             array[low] = array[high];
        }
        while (low < high && array[low] <= tmp){
                low++;
        }
         if(low >= high){
             array[low] = tmp;
             break;
         }else{
             array[high] = array[low];
         }
    }
    return low; 
}
       
(2)递归实现快速排序的代码优化

优化方法:
在数字较少时,进行直接插入排序;
较多时,先三数取中法,使数组尽可能有序,再找基准,时间复杂度会减小。

public static void quickSort1(int[] array){
        quick(array,0,array.length-1);
}
 public static void quick(int[] array,int low,int high){
        //优化
        if(high - low + 1 <= 100){
            insertSort(array,low,high);
            return;
        }
        threeNumMid(array,low,high);
        //
        int par = partion(array,low,high);
        if(par > low+1){
            quick(array,low,par-1);
        }
        if(par < high-1){
            quick(array,par+1,high);
        }
    }
    
  public static void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //三数取中
    public static void threeNumMid(int[] array,int low,int high) {
        // array[low]  array[high]  array[mid]
        int mid = (low + high)/2;
        if(array[mid] > array[low]){
            swap(array,mid,low);
        }
        if(array[mid] > array[high]){
            swap(array,high,mid);
        }
        if(array[low] > array[high]){
           swap(array,low,high);
        }
    }

    public static int partion(int[] array,int low,int high){
        int tmp = array[low];
        while(low < high){
            while(low < high && array[high] >=tmp){
                high--;
            }
            if(low >= high){
                array[low] = tmp;
                break;
            }else{
                array[low] = array[high];
            }
            while (low < high && array[low] <= tmp){
                low++;
            }
            if(low >= high){
                array[low] = tmp;
                break;
            }else{
                array[high] = array[low];
            }
        }
        return low;
    }

    //优化
    public static void insertSort(int[] array,int low,int high){
        for(int i = low;i <= high;i++){
            int tmp = array[i];
            int j = i - 1;
            for(;j >= low;j--){
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
   }
(3)非递归实现快速排序

思想:
1.先进行第一趟排序,找到基准
2.看基准的左右两边的数据是否大于等于2个,若大于,将两边的low和high都入栈
3.弹出栈顶元素给high 和 low,再进行找基准找到基准,看左右两边的元素是否大于等于2,是则入栈,循环直到栈为空

public static void quickSort(int[] array){
        Stack<Integer> stack = new Stack<>();
        int low = 0;
        int high = array.length-1;
        int par = partion(array,low,high);
        if(par > low+1){
            stack.push(low);
            stack.push(par-1);
        }
        if(par < high-1){
            stack.push(par+1);
            stack.push(high);
        }
        while (!stack.empty()){
            high = stack.pop();
            low = stack.pop();
            par = partion(array,low,high);
            if(par > low+1){
                stack.push(low);
                stack.push(par-1);
            }
            if(par < high-1){
                stack.push(par+1);
                stack.push(high);
            }
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!