[Java] java排序类 冒泡排序 选择排序 插入排序 →→→→→进入此内容的聊天室

来自 , 2019-06-28, 写在 Java, 查看 104 次.
URL http://www.code666.cn/view/6d70cb65
  1. public class Sortman {
  2.     /**
  3.      * 冒泡排序
  4.      * @param Comparable数组引用
  5.      * @param start 其实位置
  6.      * @param end  终止位置
  7.      */
  8.     public void bubbleSrot(Comparable[] a,int start,int end){
  9.         for(int i=end;i>start;i--){
  10.             for(int j=start+1;j<=i;j++){
  11.                 if(a[j-1].compareTo(a[j])>0){
  12.                     Comparable temp=a[j-1];
  13.                     a[j-1]=a[j];
  14.                     a[j]=temp;
  15.                 }
  16.             }
  17.         }
  18.     }
  19.     /**
  20.      * 选择排序
  21.      * @param Comparable数组引用
  22.      * @param start 其实位置
  23.      * @param end  终止位置
  24.      */
  25.     public void selectionSort(Comparable[] a,int start,int end){
  26.         for(int i=start;i
  27.  <end>
  28.   <=end;j++){
  29.                 if(a[j].compareTo(a[p])<0)
  30.                     p=j;
  31.             }
  32.             if(p!=i){
  33.                 Comparable temp=a[i];
  34.                 a[i]=a[p];
  35.                 a[p]=temp;
  36.             }
  37.         }
  38.     }
  39.     /**
  40.      * 插入排序
  41.      * @param Comparable数组引用
  42.      * @param start 其实位置
  43.      * @param end  终止位置
  44.      */
  45.     public void insertionSort(Comparable[] a,int start,int end){
  46.         for(int i=start+1;i<=end;i++){
  47.             Comparable val=a[i];
  48.             int j=i;
  49.             while(j>start&&val.compareTo(a[j-1])<0){
  50.                 a[j]=a[j-1];
  51.                 j--;
  52.             }
  53.             a[j]=val;
  54.         }
  55.     }
  56.     private int split(int low, int high,Comparable[] a)
  57.     {
  58.         Comparable t;
  59.         t=a[low];
  60.         while(low
  61.   <high>
  62.    0&&low
  63.    <high>
  64.     <0&&low
  65.     <high>
  66.      <end>
  67.       <=h1&&j<=h2) { if(a[i].compareTo(a[j])<0) { t[k++]=a[i++]; }else { t[k++]=a[j++]; } } while(i<=h1) { t[k++]=a[i++]; } while(j<=h2) t[k++]=a[j++]; for(i=l1,j=0;i<=h2;i++,j++) { a[i]=t[j]; } } /** * 归并排序 * @param Comparable数组引用 * @param start 其实位置 * @param end 终止位置 */ public void mergeSort(Comparable[] a,int low,int high) { int mid=(low+high)/2; if(low
  68.       <high></high>
  69.      </end>
  70.     </high>
  71.    </high>
  72.   </high>
  73.  </end>//源代码片段来自云代码http://yuncode.net
  74.                        

回复 "java排序类 冒泡排序 选择排序 插入排序"

这儿你可以回复上面这条便签

captcha