冒泡排序java

來源:互聯網
上載者:User

標籤:沒有   ati   mil   bubble   開始   冒泡   log   stat   class   

一、最簡單粗暴的排序

思想為:讓每一個關鍵字都和它後邊的每一個關鍵字比較,

如果大則交換,這樣第一個位置的關鍵字在一次迴圈後一定變為最小值。

 1 package demo01; 2  3 class BubbleSort01 { 4     public static void main(String[] args) { 5         int[] n = {0,5,2,6,9,3,4,8,1,10,82,100,52,35,46,7}; 6         int i,j; 7         for(int x=1;x<n.length;x++){ 8             System.out.print(n[x] + "、"); 9         }10         System.out.println("");11         12         for(i=1;i<n.length;i++){13             for(j=i+1;j<n.length;j++){14                 if(n[i]>n[j]){15                     int temp = n[i];16                     n[i] = n[j];17                     n[j] = temp;18                 }19             }20         }21         22         for(int x=1;x<n.length;x++)23             System.out.print(n[x]+ "、");24     }25 }

 

二、正宗的冒泡排序

思想:

當外迴圈為1(迴圈變數i=1),內迴圈(變數j)由大到小,兩兩比較,

將較小值換到前面,之道最小值放到最前面;開始下一次迴圈(i=2),一次類推…………

 

 1 package demo01; 2  3 class BubbleSort02 { 4     public static void main(String[] args) { 5         int[] n = {0,5,2,6,9,3,4,8,1}; 6         int i,j; 7         for(int x=1;x<n.length;x++){ 8             System.out.print(n[x] + "、"); 9         }10         System.out.println("");11         12         for(i=1;i<n.length;i++){13             for(j=n.length-1;j>=2;j--){14                 if(n[j]<n[j-1]){15                     int temp = n[j];16                     n[j] = n[j-1];17                     n[j-1] = temp;18                 }19             }20         }21         22         for(int x=1;x<n.length;x++)23             System.out.print(n[x]+ "、");24     }25 }

 

 

三、最佳化的冒泡排序演算法

思想:在上述演算法二的基礎上,對交換資料的步驟進行標記,即,如果發生了資料交換,標記的值就會變化。

當某次外迴圈進行時,標記位的值始終不變,說明此次迴圈沒有發生值的交換,即說明已經排序完成。

 1 package demo01; 2  3 class BubbleSort03 { 4  5     public static void main(String[] args) { 6         int[] n = {0,5,2,6,9,3,4,7,8,1}; 7          8         for(int x=1;x<n.length;x++){ 9             System.out.print(n[x] + "、");10         }11         System.out.println("");12         13         int i,j;14         boolean flag = true;    //flag作為標記15         for(i=1;i<n.length && flag;i++){16             flag = false;        //初始化flag17             for(j=n.length-1;j>=2;j--){18                 if(n[j]<n[j-1]){19                     int temp = n[j];20                     n[j] = n[j-1];21                     n[j-1] = temp;22                     flag = true;23                 }24             }25         }26         27         for(int x=1;x<n.length;x++)28             System.out.print(n[x]+ "、");29     }30 }

 

 

冒泡排序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.