快速排序

package com.ryan.demo1;

import java.lang.reflect.Array;

import javax.security.auth.kerberos.KerberosKey;

public class QuickSort {

    public static void main(String[] args) {
        int[] a= {5,2,6,0,3,9,1,7,4,8};
Qsort(a, 0, 9);
for (int i : a) {
    System.out.println(a[i]);
}
    }
    public static int partition(int[] a,int low,int high) {
        int M=low+(high-low)/2;  //優(yōu)化之一: 選取的基準(zhǔn)值要是首尾中三個值的中位數(shù),所以要對數(shù)組中的三個值進(jìn)行比較
        if(a[low]>a[high])      //把首尾大的值換到最后面
        {swap(a,low,high);}
        if(a[M]>a[high])         //把中尾大的值換到最后面
        {swap(a,M,high);}
        if(a[low]<a[M])      //把首重大的值換到最前面
        {swap(a,M,low);}
        int point;        //把第一個值當(dāng)作 基準(zhǔn)值
        point=a[low];
        while(low<high) {
            while(low<high&&point<=a[high]) {  //把數(shù)組右邊比基準(zhǔn)值大的值過濾了
                high--;                         
            }
            /*swap(a, low, high);*/           //優(yōu)化之二:point值會頻繁交換移動,所以直接賦值,將數(shù)組右邊大的值填給左邊小的數(shù)組
            a[low]=a[high];
            while(low<high&&a[low]<=point) {  //把數(shù)組右邊比基準(zhǔn)值大的值過濾了
                low++;
            }
            /*swap(a, low, high);*/
            a[high]=a[low];
        }
        a[low]=point;//補(bǔ)位,補(bǔ)最后那個空出來的位置
        return low;
    }
    public static void Qsort(int[] a,int low,int high) {
        int point;
        int length_insertsort=7;   //插入排序的長度
        if((high-low+1)>length_insertsort) {
     while(low<high)            
        {   point=partition(a,low,high);
            if(point-low<high-point)  //運(yùn)用尾遞歸,即先對數(shù)組短的進(jìn)行遞歸,如果結(jié)束了再對point賦值 跑到另一組數(shù)組進(jìn)行遞歸 
                { Qsort(a, low, point-1);
            
                       low=point+1;}   //執(zhí)行此語句時,意味著前面一句的Qsort已經(jīng)因?yàn)閘ow等于high而終止,所以此時point+1跳出當(dāng)前循環(huán),另一對數(shù)組開始執(zhí)行遞歸 
            
        else {  Qsort(a, point+1, high);
             high=point-1;
   
        }
    }}
else{
insertSort(a,low,high);
}
}
    public static void insertSort(int[] a,int low,int high) {
                int i,j;
        for(i=low+1;i<high;i++) {
              for(j=i;(j>low)&&a[j]<a[j-1];j--) {
                    swap(a,j,j-1);
                }
            
            }
        
    }
    public static void swap(int[] a,int low,int high) {
        int temp =a[high];
        a[high]=a[low];
        a[low]=temp;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 總結(jié)一下常見的排序算法。 排序分內(nèi)排序和外排序。內(nèi)排序:指在排序期間數(shù)據(jù)對象全部存放在內(nèi)存的排序。外排序:指在排序...
    jiangliang閱讀 1,520評論 0 1
  • 概述 排序有內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部...
    蟻前閱讀 5,303評論 0 52
  • 1.插入排序—直接插入排序(Straight Insertion Sort) 基本思想: 將一個記錄插入到已排序好...
    依依玖玥閱讀 1,356評論 0 2
  • “噓~聽說,下雨天會有妖怪出沒哦~” 小羽毛盯著陰沉的天空,眼睛轉(zhuǎn)向大尾巴,“你怎么知道會有妖怪,難倒你是妖怪?”...
    奔跑在森林中的貓閱讀 262評論 0 0
  • 借鑒于[美團(tuán)點(diǎn)評技術(shù)團(tuán)隊(duì)][2][2]: http://tech.meituan.com/java-hashmap...
    xx1994閱讀 462評論 0 2

友情鏈接更多精彩內(nèi)容