JAVA排序演算法實現代碼-插入排序

來源:互聯網
上載者:User
 
JAVA排序演算法實現代碼-插入排序
  1. /** 
  2.  * JAVA排序演算法實現代碼-插入排序。 
  3.  *  
  4.  * @author 老紫竹 JAVA世紀網(java2000.net) 
  5.  *  
  6.  */  
  7. public class Test {  
  8.   public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 預設資料數組  
  9.   
  10.   public static void main(String args[]) {  
  11.     int i; // 迴圈計數變數  
  12.     int Index = a.length;// 資料索引變數  
  13.   
  14.     System.out.print("排序前: ");  
  15.     for (i = 0; i < Index - 1; i++)  
  16.       System.out.print(" " + a[i] + " ");  
  17.     System.out.println("");  
  18.   
  19.     InsertSort(Index - 1); // 選擇排序  
  20.     // 排序後結果  
  21.     System.out.print("排序後: ");  
  22.     for (i = 0; i < Index - 1; i++)  
  23.       System.out.print(" " + a[i] + " ");  
  24.     System.out.println("");  
  25.   }  
  26.   
  27.   public static void InsertSort(int Index) {  
  28.     int i, j, k; // 迴圈計數變數  
  29.     int InsertNode; // 欲插入資料變數  
  30.   
  31.     for (i = 1; i < Index; i++) // 依序插入數值  
  32.     {  
  33.       InsertNode = a[i]; // 設定欲插入的數值  
  34.       j = i - 1; // 欲插入數組的開始位置  
  35.       // 找適當的插入位置  
  36.       while (j >= 0 && InsertNode < a[j]) {  
  37.         a[j + 1] = a[j];  
  38.         j--;  
  39.       }  
  40.       a[j + 1] = InsertNode; // 將數值插入  
  41.       // 列印目前排序結果  
  42.       System.out.print("排序中: ");  
  43.       for (k = 0; k < Index; k++)  
  44.         System.out.print(" " + a[k] + " ");  
  45.       System.out.println("");  
  46.     }  
  47.   }  
  48. }  
/** * JAVA排序演算法實現代碼-插入排序。 *  * @author 老紫竹 JAVA世紀網(java2000.net) *  */public class Test {  public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 預設資料數組  public static void main(String args[]) {    int i; // 迴圈計數變數    int Index = a.length;// 資料索引變數    System.out.print("排序前: ");    for (i = 0; i < Index - 1; i++)      System.out.print(" " + a[i] + " ");    System.out.println("");    InsertSort(Index - 1); // 選擇排序    // 排序後結果    System.out.print("排序後: ");    for (i = 0; i < Index - 1; i++)      System.out.print(" " + a[i] + " ");    System.out.println("");  }  public static void InsertSort(int Index) {    int i, j, k; // 迴圈計數變數    int InsertNode; // 欲插入資料變數    for (i = 1; i < Index; i++) // 依序插入數值    {      InsertNode = a[i]; // 設定欲插入的數值      j = i - 1; // 欲插入數組的開始位置      // 找適當的插入位置      while (j >= 0 && InsertNode < a[j]) {        a[j + 1] = a[j];        j--;      }      a[j + 1] = InsertNode; // 將數值插入      // 列印目前排序結果      System.out.print("排序中: ");      for (k = 0; k < Index; k++)        System.out.print(" " + a[k] + " ");      System.out.println("");    }  }}

運行結果

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

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

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

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

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

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

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

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

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

排序後: 0 1 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.