快速排序Java代碼

來源:互聯網
上載者:User

標籤:

/*講解網址http://wenku.baidu.com/link?url=1xofQsF1043cyk06D7L_OWOJUna67Lx3GNK9Qg6HbADlRH_TMnLx-3EVVlrTVUVqa9c442X576VsAlq8YTp8v7O9P7c_VTiBM7gD_08wYe3*/package quickSort;/** * 快速排序 * @author Administrator * */public class QuickSort {    void swap(int[] a,int i,int j){        int temp;        temp=a[i];        a[i]=a[j];        a[j]=temp;    }        int pos(int[] a,int low,int high){        int temp=a[low],i=low,j=high;        while(i<j){            while(i<j){                while((a[j]>=temp)&&(i<j)){                    j--;                }                if(i<j){                    swap(a,i,j);                    i++;                }                while((a[i]<=temp)&&(i<j)){                    i++;                }                if(i<j){                    swap(a,i,j);                    j--;                }            }        }        return i;    }        void quickSort(int[] a,int low,int high){        int pos;        if(low<high){            pos=pos(a,low,high);            quickSort(a,low,pos-1);            quickSort(a,pos+1,high);        }    }}package quickSort;/** * 測試類別 * @author Administrator * */public class Test {    public static void main(String[] args){        final int N=(int)(Math.random()*100+1);        QuickSort q=new QuickSort();        int[] a=new int[N];                for(int j=0;j<N;j++){            a[j]=(int)(Math.random()*100);        }                System.out.println("排序前:");        for(int i=0;i<a.length;i++){            System.out.print(a[i]+" ");        }                q.quickSort(a,0,a.length-1);        System.out.println("排序後:");        for(int i=0;i<a.length;i++){            System.out.print(a[i]+" ");        }    }}

 

快速排序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.