【数据结构】——排序算法——2.2、快速排序

前端之家收集整理的这篇文章主要介绍了【数据结构】——排序算法——2.2、快速排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

【数据结构】——排序算法——2.2、快速排序

一、先上维基的图:

图一、快速排序效果


图二、快速排序实例

分类 排序算法
数据结构 不定
最差时间复杂度
最优时间复杂度
平均时间复杂度
最差空间复杂度 根据实现的方式不同而不同
二、描述
快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。举个例子,军训时第一次集合,大家一时间混乱了,不知道怎么排,这时候教官随便抓了个人,说以该同学为基准,高的站左边,矮的站右边。此时,基本就把队伍分为高低两大阵营。那么,在每个小区间内,又进行同样的操作,迭代到每个区间内只有一个人,那么操作便结束了!

步骤为:

  1. 从数列中挑出一个元素,称为"基准"(pivot),
  2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。
  3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。

递归的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了。虽然一直递归下去,但是这个算法总会退出,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。

三、Java程序
非wiki版:
  1. public class Qsorta {
  2. public static void qsort_asc(int source[],int low,int high){
  3. int i,j,x;
  4. if(low < high){
  5. i = low;
  6. j = high;
  7. x = source[i];
  8. while(i<j){
  9. while(i<j&& source[j]>x ){
  10. j--;
  11. }
  12. if(i<j){
  13. source[i] = source[j];
  14. i++;
  15. }
  16. while(i<j&& source[i] < x){
  17. i++;
  18. }
  19. if(i<j){
  20. source[j] = source[i];
  21. j--;
  22. }
  23. }
  24. source[i] = x;
  25. qsort_asc(source,low,i-1);
  26. qsort_asc(source,i+1,high);
  27. }
  28. }
  29. public static void main(String[] args){
  30. int[] a = {4,2,1,6,3,-5,85};
  31. int i;
  32. qsort_asc(a,a.length-1);
  33. for(i=0;i<a.length-1;i++)
  34. System.out.println("a" + i + " is "+a[i]);
  35. }
  36.  
  37. }

wiki版:
  1. import java.util.Comparator;
  2. import java.util.Random;
  3. public class Quicksort {
  4. public static final Random RND = new Random();
  5. private static void swap(Object[] array,int i,int j) {
  6. Object tmp = array[i];
  7. array[i] = array[j];
  8. array[j] = tmp;
  9. }
  10. private static <E> int partition(E[] array,int begin,int end,Comparator<? super E> cmp) {
  11. int index = begin + RND.nextInt(end - begin + 1);
  12. E pivot = array[index];
  13. swap(array,index,end);
  14. for (int i = index = begin; i < end; ++ i) {
  15. if (cmp.compare(array[i],pivot) <= 0) {
  16. swap(array,index++,i);
  17. }
  18. }
  19. swap(array,end);
  20. return (index);
  21. }
  22. private static <E> void qsort(E[] array,Comparator<? super E> cmp) {
  23. if (end > begin) {
  24. int index = partition(array,begin,end,cmp);
  25. qsort(array,index - 1,index + 1,cmp);
  26. }
  27. }
  28. public static <E> void sort(E[] array,Comparator<? super E> cmp) {
  29. qsort(array,array.length - 1,cmp);
  30. }
  31. }
  32. /*
  33. * more efficient implements for quicksort. <br />
  34. * use left,center and right median value (@see #median()) for the pivot,and
  35. * the more efficient inner loop for the core of the algorithm.
  36. */
  37. class Sort {
  38. public static final int CUTOFF = 11;
  39. /**
  40. * quick sort algorithm. <br />
  41. *
  42. * @param arr an array of Comparable items. <br />
  43. */
  44. public static <T extends Comparable<? super T>> void quicksort( T[] arr ) {
  45. quickSort( arr,arr.length - 1 );
  46. }
  47. /**
  48. * get the median of the left,center and right. <br />
  49. * order these and hide the pivot by put it the end of
  50. * of the array. <br />
  51. *
  52. * @param arr an array of Comparable items. <br />
  53. * @param left the most-left index of the subarray. <br />
  54. * @param right the most-right index of the subarray.<br />
  55. * @return T
  56. */
  57. public static <T extends Comparable<? super T>> T median( T[] arr,int left,int right ) {
  58. int center = ( left + right ) / 2;
  59. if ( arr[left].compareTo( arr[center] ) > 0 )
  60. swapRef( arr,left,center );
  61. if ( arr[left].compareTo( arr[right] ) > 0 )
  62. swapRef( arr,right );
  63. if ( arr[center].compareTo( arr[right] ) > 0 )
  64. swapRef( arr,center,right );
  65. swapRef( arr,right - 1 );
  66. return arr[ right - 1 ];
  67. }
  68. /**
  69. * internal method to sort the array with quick sort algorithm. <br />
  70. *
  71. * @param arr an array of Comparable Items. <br />
  72. * @param left the left-most index of the subarray. <br />
  73. * @param right the right-most index of the subarray. <br />
  74. */
  75. private static <T extends Comparable<? super T>> void quickSort( T[] arr,int right ) {
  76. if ( left + CUTOFF <= right ) {
  77. //find the pivot
  78. T pivot = median( arr,right );
  79. //start partitioning
  80. int i = left,j = right - 1;
  81. for ( ; ; ) {
  82. while ( arr[++i].compareTo( pivot ) < 0 ) ;
  83. while ( arr[--j].compareTo( pivot ) > 0 ) ;
  84. if ( i < j )
  85. swapRef( arr,i,j );
  86. else
  87. break;
  88. }
  89. //swap the pivot reference back to the small collection.
  90. swapRef( arr,right - 1 );
  91. quickSort( arr,i - 1 ); //sort the small collection.
  92. quickSort( arr,i + 1,right ); //sort the large collection.
  93. } else {
  94. //if the total number is less than CUTOFF we use insertion sort instead (cause it much more efficient).
  95. insertionSort( arr,right );
  96. }
  97. }
  98. /**
  99. * method to swap references in an array.<br />
  100. *
  101. * @param arr an array of Objects. <br />
  102. * @param idx1 the index of the first element. <br />
  103. * @param idx2 the index of the second element. <br />
  104. */
  105. public static <T> void swapRef( T[] arr,int idx1,int idx2 ) {
  106. T tmp = arr[idx1];
  107. arr[idx1] = arr[idx2];
  108. arr[idx2] = tmp;
  109. }
  110. /**
  111. * method to sort an subarray from start to end
  112. * with insertion sort algorithm. <br />
  113. *
  114. * @param arr an array of Comparable items. <br />
  115. * @param start the begining position. <br />
  116. * @param end the end position. <br />
  117. */
  118. public static <T extends Comparable<? super T>> void insertionSort( T[] arr,int start,int end ) {
  119. int i;
  120. for ( int j = start + 1; j <= end; j++ ) {
  121. T tmp = arr[j];
  122. for ( i = j; i > start && tmp.compareTo( arr[i - 1] ) < 0; i-- ) {
  123. arr[ i ] = arr[ i - 1 ];
  124. }
  125. arr[ i ] = tmp;
  126. }
  127. }
  128. }

猜你在找的数据结构相关文章