快速排序是一種非常高效的排序算法,它采用“分而治之”的思想,把大的拆分成小的,小的再拆分為更小的。
其原理如下:
對一組給定的記錄,通過一趟排序后,將原序列分為兩部分,其中前一部分的所有數據均比后一部分的所有的記錄小,然后依次對前后兩部分的記錄進行快速排序,遞歸該過程,直到序列中的所有記錄均有序為止。
快速排序的 java 實現:

1 package com.mianshi.easy; 2 3 public class TestQuickSort { 4 5 public static void quickSort(int[] a){ 6 sort(a, 0, a.length-1); 7 } 8 9 public static void sort(int[] a, int low, int high){10 11 int i, j;12 int index;13 14 if(low >= high){15 return;16 }17 18 i = low;19 j = high;20 index = a[i];21 while(i<j){22 while(i<j && a[j]>=index){23 j--;24 }25 if(i<j)26 a[i++] = a[j];27 while(i<j && a[i]<index){28 i++;29 }30 if(i<j){31 a[j--]=a[i];32 }33 }34 a[i] = index;35 sort(a, low, i-1);36 sort(a, i+1, high);37 }38 39 40 public static void main(String[] args) {41 42 int[] a = {6,5,3,9,0,5,1,2,4,7,8};43 //快速排序44 quickSort(a);45 for(int i = 0;i < a.length; i++){46 System.out.View Code結果:
0 1 2 3 4 5 5 6 7 8 9
算法穩定性:不穩定的排序算法。
時間復雜度:平均時間復雜度為O(nlogn)

優秀帖子幫助理解:
http://blog.csdn.net/morewindows/article/details/6684558
新聞熱點
疑難解答