数组排序总结(冒泡,选择,插入,希尔)

  1.    1package org.idcn.jse;  
  2.    2.   
  3.    3public class SortAll {  
  4.    4.   
  5.    5.  /** 
  6.    6.   * 冒泡排序,选择排序,插入排序,希尔(Shell)排序 Java的实现  
  7.    9.   */  
  8.   10.  public static void main(String[] args) {  
  9.   11.   int[] i = { 15612493233940359687 };  
  10.   12.   System.out.println("----冒泡排序的结果:");  
  11.   13.   maoPao(i);  
  12.   14.   System.out.println();  
  13.   15.   System.out.println("----选择排序的结果:");  
  14.   16.   xuanZe(i);  
  15.   17.   System.out.println();  
  16.   18.   System.out.println("----插入排序的结果:");  
  17.   19.   chaRu(i);  
  18.   20.   System.out.println();  
  19.   21.   System.out.println("----希尔(Shell)排序的结果:");  
  20.   22.   shell(i);  
  21.   23.  }  
  22.   24.   
  23.   25.  // 冒泡排序  
  24.   26.  public static void maoPao(int[] x) {  
  25.   27.   for (int i = 0; i < x.length; i++) {  
  26.   28.    for (int j = i + 1; j < x.length; j++) {  
  27.   29.     if (x[i] > x[j]) {  
  28.   30.      int temp = x[i];  
  29.   31.      x[i] = x[j];  
  30.   32.      x[j] = temp;  
  31.   33.     }  
  32.   34.    }  
  33.   35.   }  
  34.   36.   for (int i : x) {  
  35.   37.    System.out.print(i + " ");  
  36.   38.   }  
  37.   39.  }  
  38.   40.   
  39.   41.  // 选择排序  
  40.   42.  public static void xuanZe(int[] x) {  
  41.   43.   for (int i = 0; i < x.length; i++) {  
  42.   44.    int lowerIndex = i;  
  43.   45.    // 找出最小的一个索引  
  44.   46.    for (int j = i + 1; j < x.length; j++) {  
  45.   47.     if (x[j] < x[lowerIndex]) {  
  46.   48.      lowerIndex = j;  
  47.   49.     }  
  48.   50.    }  
  49.   51.    // 交换  
  50.   52.    int temp = x[i];  
  51.   53.    x[i] = x[lowerIndex];  
  52.   54.    x[lowerIndex] = temp;  
  53.   55.   }  
  54.   56.   for (int i : x) {  
  55.   57.    System.out.print(i + " ");  
  56.   58.   }  
  57.   59.  }  
  58.   60.   
  59.   61.  // 插入排序  
  60.   62.  public static void chaRu(int[] x) {  
  61.   63.   for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦  
  62.   64.    for (int j = i; j > 0; j--) {  
  63.   65.     if (x[j] < x[j - 1]) {  
  64.   66.      int temp = x[j];  
  65.   67.      x[j] = x[j - 1];  
  66.   68.      x[j - 1] = temp;  
  67.   69.     }  
  68.   70.    }  
  69.   71.   }  
  70.   72.   for (int i : x) {  
  71.   73.    System.out.print(i + " ");  
  72.   74.   }  
  73.   75.  }  
  74.   76.   
  75.   77.  // 希尔排序  
  76.   78.  public static void shell(int[] x) {  
  77.   79.   // 分组  
  78.   80.   for (int increment = x.length / 2; increment > 0; increment /= 2) {  
  79.   81.    // 每个组内排序  
  80.   82.    for (int i = increment; i < x.length; i++) {  
  81.   83.     int temp = x[i];  
  82.   84.     int j = 0;  
  83.   85.     for (j = i; j >= increment; j -= increment) {  
  84.   86.      if (temp < x[j - increment]) {  
  85.   87.       x[j] = x[j - increment];  
  86.   88.      } else {  
  87.   89.       break;  
  88.   90.      }  
  89.   91.     }  
  90.   92.     x[j] = temp;  
  91.   93.    }  
  92.   94.   }  
  93.   95.   
  94.   96.   for (int i : x) {  
  95.   97.    System.out.print(i + " ");  
  96.   98.   }  
  97.   99.  }  
  98.  100. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值