码迷,mamicode.com
首页 > 编程语言 > 详细

java常用四种排序源代码

时间:2015-05-22 02:04:29      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:源代码   排序   

选择排序

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);

             }

      }

}


本文出自 “亘古之焱” 博客,请务必保留此出处http://610201092.blog.51cto.com/7852003/1653630

java常用四种排序源代码

标签:源代码   排序   

原文地址:http://610201092.blog.51cto.com/7852003/1653630

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!