java排序之冒泡排序

來源:互聯網
上載者:User

標籤:rgs   gpo   body   複雜   log   system   sys   for   main   

代碼:

 1 public class Test { 2     public static int[] bublle_sort(int a[]){ 3         for (int i = 0; i < a.length-1; i++) { 4             boolean exchange = false; 5             for (int j = 0; j < a.length-i-1; j++) { 6                 if (a[j+1]<a[j]){ 7                     int tmp = a[j]; 8                     a[j] = a[j+1]; 9                     a[j+1]=tmp;10                     exchange = true;11                 }12             13             }14             if (exchange = false){15                 break;16             }17         }18         return a;19     }20 21 public static void main(String[] args) {22     //冒泡排序23     System.out.println("************before bublle_sort**************");24     int a[] = new int[5];25     for (int i = 0; i < a.length; i++) {26         a[i] = (int)(Math.random()*100);27         System.out.println("未經處理資料:"+a[i]);28     }29     System.out.println("************after bublle_sort**************");30     bublle_sort(a);31     for (int i = 0; i < a.length; i++) {32         System.out.println(a[i]);33     }34         35 }

冒泡排序的時間複雜度位O(n^2),但是理想情況下為O(n),記憶口訣:外管輪流內管排

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.