快速排序法 quickSort---java

來源:互聯網
上載者:User

標籤:

快速排序法:

public class Main {    public static void main(String[] args) {          int a[]={7,8,1,3,5};          new Main(a);    }     public Main(int[] a){         System.out.println("排序前:");         print(a);         quickSort(a,0,a.length-1);         System.out.println();         System.out.println("排序後:");         print(a);     }     public int getMiddle(int[] list,int low,int high){            int temp=list[low];//數組的第一個作為中軸            while(low<high){                while(low < high && list[high]>=temp) high--;                list[low]=list[high];//比中軸小的數移到低端                while(low < high && list[low]<=temp) low++;                list[high]=list[low];//比中軸大的移動到高端            }           list[low]=temp;//中軸記錄到為尾            // System.out.println("low==high? "+(low==high)+"low= "+low+"high= "+high);           return low;//返回中軸位置           //判斷得知,最後三個中軸為3 2 0     }     public void quickSort(int a[],int low,int high){         if(low<high){             int middle=getMiddle(a, low, high);//將數組a一分為二             quickSort(a, low, middle-1);//對小於中軸的部分遞迴排序             quickSort(a, middle+1, high);//對大於中軸的部分遞迴排序         }     }      void print(int []a){         for(int s: a)             System.out.print(s+" , ");        }}   
本演算法應做到信手拈來。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

快速排序法 quickSort---java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.