JAVA排序演算法實現代碼-快速(Quick Sort)排序

來源:互聯網
上載者:User
 
JAVA排序演算法實現代碼-冒泡(Bubble Sort)排序
  1. /** 
  2.  * JAVA排序演算法實現代碼-冒泡(Bubble Sort)排序。 
  3.  *  
  4.  * @author 老紫竹 JAVA世紀網(java2000.net) 
  5.  *  
  6.  */  
  7. public class Test {  
  8.   public static void main(String[] args) {  
  9.     int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 };  
  10.   
  11.     System.out.print("排序前: ");  
  12.   
  13.     for (int i = 0; i < a.length; i++)  
  14.       System.out.printf("%3s", a[i]);  
  15.   
  16.     System.out.println();  
  17.   
  18.     Test test = new Test();  
  19.     test.bubbleSort(a);  
  20.   
  21.     System.out.print("排序後: ");  
  22.   
  23.     for (int i = 0; i < a.length; i++)  
  24.       System.out.printf("%3s", a[i]);  
  25.   
  26.     System.out.println();  
  27.   }  
  28.   
  29.   public void bubbleSort(int[] a) {  
  30.     int len = a.length;  
  31.   
  32.     System.out.println("數組大小是:" + len);  
  33.   
  34.     boolean change = false;  
  35.     int temp;  
  36.     int count = 0;  
  37.   
  38.     for (int i = len; i > 1; i--) {  
  39.       for (int j = 0; j < i - 1; j++) {  
  40.         if (a[j + 1] < a[j]) {  
  41.           temp = a[j + 1];  
  42.           a[j + 1] = a[j];  
  43.           a[j] = temp;  
  44.           change = true;  
  45.           count++;  
  46.         }  
  47.   
  48.       }  
  49.       if (change) {  
  50.         System.out.print("第" + count + "趟交換:  ");  
  51.         for (int k = 0; k < len; k++)  
  52.           System.out.print(a[k] + " ");  
  53.   
  54.         System.out.println();  
  55.       }  
  56.     }  
  57.   }  
  58. }  
/** * JAVA排序演算法實現代碼-冒泡(Bubble Sort)排序。 *  * @author 老紫竹 JAVA世紀網(java2000.net) *  */public class Test {  public static void main(String[] args) {    int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 };    System.out.print("排序前: ");    for (int i = 0; i < a.length; i++)      System.out.printf("%3s", a[i]);    System.out.println();    Test test = new Test();    test.bubbleSort(a);    System.out.print("排序後: ");    for (int i = 0; i < a.length; i++)      System.out.printf("%3s", a[i]);    System.out.println();  }  public void bubbleSort(int[] a) {    int len = a.length;    System.out.println("數組大小是:" + len);    boolean change = false;    int temp;    int count = 0;    for (int i = len; i > 1; i--) {      for (int j = 0; j < i - 1; j++) {        if (a[j + 1] < a[j]) {          temp = a[j + 1];          a[j + 1] = a[j];          a[j] = temp;          change = true;          count++;        }      }      if (change) {        System.out.print("第" + count + "趟交換:  ");        for (int k = 0; k < len; k++)          System.out.print(a[k] + " ");        System.out.println();      }    }  }}

運行結果

排序前: 10 32 1 9 5 7 12 0 4 3

數組大小是:10

第8趟交換: 10 1 9 5 7 12 0 4 3 32

第15趟交換: 1 9 5 7 10 0 4 3 12 32

第20趟交換: 1 5 7 9 0 4 3 10 12 32

第23趟交換: 1 5 7 0 4 3 9 10 12 32

第26趟交換: 1 5 0 4 3 7 9 10 12 32

第29趟交換: 1 0 4 3 5 7 9 10 12 32

第31趟交換: 0 1 3 4 5 7 9 10 12 32

第31趟交換: 0 1 3 4 5 7 9 10 12 32

第31趟交換: 0 1 3 4 5 7 9 10 12 32

排序後: 0 1 3 4 5 7 9 10 12 32

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.