java常用四种排序源代码

选择排序

public class ChooseSort {

      publicstatic voidmain(String[] args) {

             int[]x = { 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 };

             ChooseSort cs = new ChooseSort();

             cs.selectSort(x);

             for(int i = 0; i < x.length; i++) {

                    System.out.print(x[i] + "  ");

             }

      }

 

      publicstatic voidselectSort(int[] a) {

             intminIndex = 0;

             inttemp = 0;

             if((a == null) || (a.length == 0))

                    return;

             for(int i = 0; i < a.length; i++) {

                    minIndex = i;// 无序区的最小数据数组下标

                    for(int j = i + 1; j < a.length; j++) {// 在无序区中找到最小数据并保存其数组下标

                           if (a[j] < a[minIndex]) {

                                  minIndex = j;

                           }

                    }

                    if(minIndex != i) {// 如果不是无序区的最小值位置不是默认的第一个数据,则交换之。

                           temp = a[i];

                           a[i] = a[minIndex];

                           a[minIndex] = temp;

                    }

             }

      }

}


插入排序

public class InsertSort {

      publicstatic voidmain(String[] args) {

             inta[]={ 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 };;

             InsertSort is=new InsertSort();

             is.sort(a);

             for(int i=0;i<a.length;i++){

                    System.out.print(a[i]+"  ");

             }

      }

      publicvoid sort(intobj[]){

             for(int j=1;j<obj.length;j++){

                    intkey=obj[j];

                    inti=j-1;

                    while(i>=0&&obj[i]>key){

                           obj[i+1]=obj[i];

                           i--;

                    }

                    obj[i+1]=key;

             }

      }

}

 

冒泡排序

public class BubbleSort {

      publicvoid bubbleSort(int a[]){

             intn=a.length;

             for(int i=0;i<n-1;i++){

                    for(int j=0;j<n-i-1;j++){

                           if(a[j]>a[j+1]){

                                  int temp=a[j];

                                  a[j]=a[j+1];

                                  a[j+1]=temp;

                           }

                    }

             }

      

      publicstatic voidmain(String[] args){

             BubbleSort bs=new BubbleSort();

             inta[]={ 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 };

             bs.bubbleSort(a);

             for(int m = 0; m < a.length; m++) {

                    System.out.print(a[m] + "  ");

             }

      }

}

  

快速排序

public class QuickSort {

      publicstatic voidmain(String[] args) {

             int[]strVoid = { 2, 332,16, 575, 203, 4, 23, 11, 345, 32 };

             QuickSort sort = new QuickSort();

             sort.quickSort(strVoid, 0, strVoid.length - 1);

             for(int m = 0; m < strVoid.length; m++) {

                    System.out.print(strVoid[m] + "  ");

             }

      }

      publicvoid quickSort(int strDate[], intleft, int right) {

             inti, j;

             i = left;

             j = right;

             intmiddle = strDate[(i)];

             while(i < j) {

                    inttempDate = 0;

                    while(i < j && strDate[j] >= middle) {

                           j = j - 1;

                    }

                    tempDate = strDate[j];

                    strDate[j] = strDate[i];

                    strDate[i] = tempDate;

                    while(i < j && strDate[i] < middle) {

                           i = i + 1;

                    }

                    tempDate = strDate[j];

                    strDate[j] = strDate[i];

                    strDate[i] = tempDate;

             }

             strDate[i] = middle;

             if(left < i - 1) {

                    quickSort(strDate, left, i -1);

             }

             if(right > i + 1) {

                    quickSort(strDate, i + 1,right);

             }

      }

}


你可能感兴趣的:(java,排序,源代码)