java数据结构----经典排序集锦

来源:互联网 发布:贵阳软件人均工资 编辑:程序博客网 时间:2024/05/18 13:48

几种排序的对比!!

1. 冒泡排序

package com.jzm.kindsOfSort;public class BubbleSort {/** * @param args   * 冒泡排序 */  public static void bubbleSort(Comparable a[]){       for(int i=1;i<a.length;i++){         for(int j=0;j<a.length-1;j++)     {    if(a[j].compareTo(a[j+1]) > 0){    Comparable t = a[j];    a[j] = a[j+1];    a[j+1] = t;    }    }        }    }  public static void main(String[] args) {     Integer[] pData = {100,10,82,20,70,6,8,99,11,165};                   bubbleSort(pData);         System.out.println("最后结果:");       for (int i = 0; i < pData.length; i++) {       System.out.print(pData[i]+" ");}}}


2. InsertSort

package com.jzm.kindsOfSort;public class InsertionSort{  //简单插入排序public static  void insertSort(Comparable []data){       for(int i=1;i<data.length;i++)    {        Comparable tmp = data[i];                  if(tmp.compareTo(data[i-1]) < 0)        {            int j;            for(j=i-1; j>=0 && tmp.compareTo(data[j])<0;j--)            {               data[j+1] = data[j];            }                data[j+1] = tmp;         }    }}public  void display(Comparable a[]){for (int i = 0; i < a.length; i++) {System.out.print(a[i]+" ");}}public static void main(String[] args){ Comparable a[] = {2,4,5,6,7,100,0,25,1};  InsertionSort insertionSort = new InsertionSort(); insertionSort.insertSort(a); insertionSort.display(a);}}


3. quickSort

package com.jzm.kindsOfSort;public class QuickSort{     //快排一public static  void quickSort(Integer[] a, int left, int right) {           int  i= left, j= right;           int  middle,strTemp;                   middle = a[(left + right)/2];           do {               while ((a[i] < middle) && (i < right))                    i++;               while ((a[j] > middle) && (j > left))                    j--;               if (i <= j){                   strTemp = a[i];                   a[i] = a[j];                   a[j] = strTemp;                   i++;                   j--;               }                } while (i <= j);            /*             System.out.println("left:"+left+" right:"+right);               for (int t = 0; t < a.length; t++)           System.out.print(a[t] + " ");              System.out.println("");*/                if (left < j) {               quickSort(a, left, j);           }             if (right > i)               quickSort(a, i, right);        }       public static void main(String[] argv) {             Integer [] pData = {10,82,20,70,6,8,99,11,165};                    quickSort(pData, 0, pData.length - 1);              System.out.println("最后结果:");       for (int i = 0; i < pData.length; i++) {       System.out.print(pData[i]+" ");}    }   }  


4. quickSort2

package com.jzm.kindsOfSort;public class QuickSort2{          //通用快排,速度慢些    private static void swap(Comparable a[],int i,int j){Comparable t = a[i];a[i] =a[j];a[j] = t;} private static int partion(Comparable[] a, int left, int right){ int i=left; int j = right+1;  Comparable x = a[left];           while(true){    while(a[++i].compareTo(x)<0 && i<right);   while(a[--j].compareTo(x) > 0);   if (i>=j) break;   swap(a,i,j);  }  a[left] = a[j]; a[j] = x; return j;}public static Comparable[] qSort(Comparable[] a, int left, int right) {                 if (left < right){  int q = partion(a,left,right);   //分段      /* System.out.println("left:"+left+" right:"+right);                    for (int t = 0; t < a.length; t++)           System.out.print(a[t] + " ");              System.out.println("");*/     qSort(a,left, q-1);   qSort(a,q+1,right);  }        return a;              }              public static void main(String[] args) {          Comparable [] a= {1,10,82,20,70,6,8,99,11,165};               Comparable [] t = qSort(a,0,a.length-1);              System.out.println("最后结果:");             for (int i = 0; i < t.length; i++) {    System.out.print(a[i]+" ");}    }   }  


5. ShellSort

package com.jzm.kindsOfSort;public class ShellSort{    //希尔排序     public  static void sort(int[]a,int dk){              int i,j,temp;                 for(i=dk; i<a.length; i++){                if(a[i]<a[i-dk]){                   temp=a[i];                   a[i]=a[i-dk];                   for(j=i; j>0 && temp<a[j-1]; j=j-dk){                       a[j]=a[j-1];                   }                    a[j]=temp;               }           }       }         public static void main(String args[]){            int[] a= {2,4,1,5,6,8,7,10,0,11,12,    1,2,1,2,11,21,2,12,12,12,12,    100,12,1,2,0,21,2,1,231,1,1,132};                int w=1;              while(w <=a.length/5){           if (w%2 == 0){              //提高效率O(n^1.5) w=w+1; System.out.println("w"); }             sort(a,w);                w=w*5+1;           }                   for(int i=0;i<a.length;i++)              System.out.print(a[i]+" ");                  }   }


6.  simpleSelectSort

package com.jzm.kindsOfSort;public class SimpleSelectSort{      //简单选择排序public static void sort(Comparable[] data) {for (int i = 0; i < data.length; i++) {      // 记录当前位置     int position = i;  //找出最小的数,并用position指向最小数的位置for (int j = i + 1; j < data.length; j++) {if (data[position].compareTo(data[j])>0){   position = j;}// end if  }                          // 交换最小数data[position]和第i位数的位置      Comparable temp = data[i]; data[i] = data[position]; data[position] = temp;}// end for  }// end sort             public static void  main(String[] args){         // 在JDK1.5版本以上,基本数据类型可以自动装箱       //int,double等基本类型的包装类已实现了Comparable接口                Comparable[] c = {4,9,23,145,27,5,2,1};    sort(c);    for (Comparable data: c)  System.out.print(data+" ");     }}


7. 排序速度测试

 

package com.jzm.kindsOfSort;import java.util.Random;public class TestSpeed{               /** * 测试各种排序的速度,其中对于大数来说,快排速度最快 */    private static final int MAXNUM = 50000;       private static boolean  isSuccess(Comparable a[]){        for (int i = 0; i < a.length-2; i++) {      if(a[i].compareTo(a[i+1]) > 0){      System.out.println("数组a没有按照降序排列");      return false;      }    }                System.out.println("检测通过");       return true;    }      public static void main(String[] args) {     SystemDate  sysDate = new SystemDate();     String      strStartTime=sysDate.getThisDate();         DebugRunTime  debugRunTime = new DebugRunTime();             Random random = new Random();                 Integer a[] = new Integer[MAXNUM];                  for(int i=0;i<MAXNUM;i++){           a[i] = random.nextInt(MAXNUM);                 }     //给a[]数组赋予初值                        // InsertionSort.insertSort(a);             //简单插入排序24275 ms.      // SimpleSelectSort.sort(a);                //简单选择排序34945 ms.                 QuickSort2.qSort(a, 0, a.length-1);      //40--52ms               //  QuickSort.quickSort(a, 0, a.length-1);   //30-50ms                    //BubbleSort.bubbleSort(a);                 //68079--70356ms        isSuccess(a);                String strStopTime=sysDate.getThisDate();    System.out.println("程序启动于:"+strStartTime);    System.out.println("程序结束于:"+strStopTime);     debugRunTime.ExecutionTime();  //后台打出运行时间}}


8. 外加测试用时类

package com.jzm.kindsOfSort;import java.util.Date; import java.text.SimpleDateFormat; import java.text.ParseException; public class RunTimeCalc{ public static void main(String[] str) {    SystemDate sysDate = new SystemDate();    String     strStartTime=sysDate.getThisDate();    DebugRunTime debugRunTime =  new DebugRunTime();       for(int i=0;i<5000;i++)    {          for(int j=0;j<1000;j++)     {          System.out.println("i="+i+",j="+j);// I/O操作非常耗时         }       }       String strStopTime=sysDate.getThisDate();    System.out.println("程序启动于:"+strStartTime);    System.out.println("程序结束于:"+strStopTime);     debugRunTime.ExecutionTime();  //后台打出运行时间           }}  class DebugRunTime {   private  long startTime = 0;   private long endtime = 0 ;   public DebugRunTime()  {      System.out.println("Begin.............................. ");  this.setStartTime(System.currentTimeMillis());   }   public void ExecutionTime() {     this.setEndtime(System.currentTimeMillis());     System.out.println("Execution time: " + ( this.getEndtime() - this.getStartTime()) + " ms.");   }   public void ExecutionTime(String message){     this.setEndtime(System.currentTimeMillis());     System.out.println(message + " Execution time: " + ( this.getEndtime() - this.getStartTime()) + " ms.");   }   private long getEndtime() {      return endtime;   }   private long getStartTime() {      return startTime;   }   private void setEndtime(long endtime) {      this.endtime = endtime;   }   private void setStartTime(long startTime)  {      this.startTime = startTime; }}  //一个日期操作类class SystemDate {  public static SystemDate systemdate = null;  public Date date;  public String formatType = "yyyy/MM/dd-hh:mm:ss";  //默认时间格式  public SimpleDateFormat fmat;  public SystemDate() {}     static SystemDate getInstance() {   if (systemdate == null)   systemdate = new SystemDate();   return systemdate;  }    //返回当前时间的字符串,用默认时间格式formatType  public String getThisDate() {   return getThisDate(formatType);  }    //返回输入时间的字符串,用默认时间格式formatType  public String getThisDate(Date date) {   return getThisDate(formatType, date);  }    //返回当前时间的字符串,时间格式为输入的参数(String formatType)  public String getThisDate(String formatType) {   date = new Date();   return getThisDate(formatType, date);  }   //返回输入时间的字符串,时间格式为输入的参数(String formatType)  public String getThisDate(String formatType, Date date) {   fmat = new SimpleDateFormat(formatType);   return fmat.format(date);  }    //返回时间Date,用输入的时间字符串转化,输入的时间格式为默认时间格式formatType   Date getAsStringDate(String strdate) throws ParseException {   return getAsStringDate(strdate, formatType);  }     //返回时间Date,用输入的时间字符串转化,输入的时间格式为String formatType  Date getAsStringDate(String strdate, String formatType) throws ParseException {  fmat = new SimpleDateFormat(formatType);   return fmat.parse(strdate);  } } 


 

9. 总结:      快排排序500万个数, 大概只要6s 钟左右。

                         如有不对的地方,请斧正。

 

 

原创粉丝点击