幾種排序的對比!!
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版本以上,基礎資料型別 (Elementary Data Type)可以自動裝箱 //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 鐘左右。
如有不對的地方,請斧正。