快速排序
原理是找出一個元素(理論上可以隨便找一個)作為基准(pivot),然后對數組進行分區操作,使基准左邊元素的值都不大於基准值,基准右邊的元素值 都不小於基准值,如此作為基准的元素調整到排序后的正確位置。遞歸快速排序,將其他n-1個元素也調整到排序后的正確位置。最后每個元素都是在排序后的正 確位置,排序完成。所以快速排序算法的核心算法是分區操作,即如何調整基准的位置以及調整返回基准的最終位置以便分治遞歸。
冒泡排序算法
原理是臨近的數字兩兩進行比較,按照從小到大或者從大到小的順序進行交換,
這樣一趟過去后,最大或最小的數字被交換到了最后一位,
然后再從頭開始進行兩兩比較交換,直到倒數第二位時結束
雙向冒泡排序算法
以整數升序排序為例來簡單說明一下雙向冒泡排序的過程:首先從前往后把最大數移到最后,然后反過來從后往前把最小的一個數移動到數組最前面,這一過程就是第一輪,然后重復這一過程,最終就會把整個數組從小到大排列好。雙向冒泡排序要稍微優於傳統的冒泡排序,因為雙向排序時數組的兩頭都排序好了,我們只需要處理數組的中間部分即可,而單向即傳統的冒泡排序只有尾部的元素是排好序的,這時每輪處理都需要從頭一直處理到已經排好序元素的前面一個元素。雖然它在效率上有了點改進,但它也不能大幅度提高其排序的效率,這是由冒泡排序的基本過程所決定了的。在此基礎上改進了一下,下面的代碼可以實現對奇數偶數分別排序
源代碼:

1 package com.zc.manythread; 2 3 public class Sort implements Runnable{ 4 int[] date; 5 public Sort (int[] date) { 6 super(); 7 this.date=date; 8 } 9 public void sort() { 10 throw new RuntimeException("please do it in subclass"); 11 } 12 @Override 13 public void run() { 14 // TODO Auto-generated method stub 15 long start=System.nanoTime(); 16 sort(); 17 long end=System.nanoTime(); 18 System.out.println(Thread.currentThread().getName()+"執行時間:"+(end-start)+"ns"); 19 } 20 21 }

package com.zc.manythread; /** * 快速排序 * @author Administrator * */ public class QSort extends Sort{ public void sort() { try { } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public QSort(int[] date) { super(date); // TODO Auto-generated constructor stub } private void swap(int a[],int i,int j) { int T; T=a[i]; a[i]=a[j]; a[j]=T; } /******************* * * @param a * @param lo0 * @param hi0 * @return */ int[] QuickSort(int a[],int lo0,int hi0){ int lo=lo0; int hi=hi0; int mid; if (hi0>lo0) { mid=a[(hi0+lo0)/2]; while(lo<=hi){ while((lo<hi0)&&(a[lo]<mid)) ++lo; while((hi>lo0)&&(a[hi]>mid)) --hi; if (lo<=hi) { swap(a,lo,hi); ++lo; --hi; } } if (lo0<hi) { QuickSort(a, lo0, hi); } if (lo<hi0) { QuickSort(a, lo, hi0); } } return a; } /************** * * 創建數組數據 * *****************/ private static int[] createDate(int count) { int[] data=new int[count]; for (int i = 0; i < data.length; i++) { data[i]=(int)(Math.random()*count); } return data; } /**************主函數*****************/ public static void main(String[] args) { final int count=100; int[] data=createDate(count); for (int n:data) { System.out.print(n+"\t"); } QSort data1=new QSort(data); System.out.println(); int[] a=data1.QuickSort(data,0, count-1); for (int n:a) { System.out.print(n+"\t"); } } }

1 package com.zc.manythread; 2 /** 3 * 冒泡排序 4 * @author Administrator 5 * 6 */ 7 8 public class BSrot extends Sort{ 9 10 public void sort() { 11 try { 12 sort(date); 13 } catch (Exception e) { 14 // TODO: handle exception 15 e.printStackTrace(); 16 } 17 } 18 public int[] sort(int[] a)throws Exception{ 19 for (int i = a.length; --i>=0;) { 20 boolean swapped=false; 21 for (int j = 0; j < i; j++) { 22 if (a[j]>a[j+1]) { 23 int T=a[j]; 24 a[j]=a[j+1]; 25 a[j+1]=T; 26 swapped=true; 27 } 28 } 29 if (!swapped) { 30 return a; 31 } 32 } 33 return a; 34 } 35 public BSrot(int[] date) { 36 super(date); 37 // TODO Auto-generated constructor stub 38 } 39 40 }

1 package com.zc.manythread; 2 /** 3 * 雙向冒泡排序 4 * @author Administrator 5 * 6 */ 7 public class BBSort extends Sort{ 8 9 public BBSort(int[] date) { 10 super(date); 11 // TODO Auto-generated constructor stub 12 } 13 14 public void sort() { 15 try { 16 sort(date); 17 } catch (Exception e) { 18 // TODO: handle exception 19 e.printStackTrace(); 20 } 21 22 } 23 24 void sort(int[] a)throws Exception{ 25 int j; 26 int limit=a.length; 27 int st=-1; 28 while(st<limit){ 29 st++; 30 limit--; 31 boolean swapped=false; 32 for (j = st ; j < limit; j++) { 33 if (a[j]>a[j+1]) { 34 int T=a[j]; 35 a[j]=a[j+1]; 36 a[j+1]=T; 37 swapped=true; 38 } 39 } 40 41 if (!swapped) { 42 return; 43 }else { 44 swapped=false; 45 for (j = limit; --j>=st;) { 46 if(a[j]>a[j+1]){ 47 int T=a[j]; 48 a[j]=a[j+1]; 49 a[j+1]=T; 50 swapped=true; 51 } 52 } 53 if (!swapped) { 54 return; 55 } 56 } 57 } 58 } 59 60 }

1 package com.zc.manythread; 2 3 public class MainTest { 4 5 private static int[] createDate(int count) { 6 int[] data=new int[count]; 7 for (int i = 0; i < data.length; i++) { 8 data[i]=(int)(Math.random()*count); 9 } 10 return data; 11 } 12 public static void main(String[] args) { 13 final int count=10000; 14 int[] data=createDate(count); 15 16 Thread qsort=new Thread(new QSort(data),"快速排序"); 17 int[] data2=new int[count]; 18 System.arraycopy(data,0,data2,0,count); 19 Thread bsort=new Thread(new BSrot(data2),"冒泡排序"); 20 int[] data3=new int[count]; 21 System.arraycopy(data,0,data3,0,count); 22 Thread bbsort=new Thread(new BBSort(data3),"雙向排序"); 23 qsort.start(); 24 bsort.start(); 25 bbsort.start(); 26 } 27 }
運行結果:
從結果來看 可以知道快速排序 效率更快!