快速排序(Quicksort),因其排序之快而得名,虽然Ta的平均时间复杂度也是O(nlgn),但是从后续仿真结果看,TA要比归并排序和堆排序都要快。

快速排序也用到了分治思想。

(一)算法实现

 1 protected void quicksort(int[] array, int first, int last) {
 2 
 3         int pivot = array[first];
 4         int i = first;
 5         int j = last - 1;
 6         boolean serachBig = true;
 7         while (i < j) {
 8             if (serachBig) {
 9                 if (array[j] < pivot) {
10                     array[i] = array[j];
11                     i++;
12                     serachBig = false;
13                 } else {
14                     j--;
15                 }
16             } else {
17                 if (array[i] > pivot) {
18                     array[j] = array[i];
19                     j--;
20                     serachBig = true;
21                 } else {
22                     i++;
23                 }
24             }
25         }
26         array[i] = pivot;
27 
28         if (i - first > 1) {
29             quicksort(array, first, i);
30         }
31         if (last - i > 2) {
32             quicksort(array, i + 1, last);
33         }
34     }
Quicksort

1)快速排序的平均时间复杂度也是O(nlgn)

2)考虑递归,快速排序空间复杂度是O(logn),不是原地排序

3)快速排序属于比较排序

4)快速排序不是稳定排序算法

(二)仿真结果

**************************************************
Number to Sort is:2500
Array to sort is:{378132,303655,213274,506865,348563,122685,857064,624884,376943,281167...}
Cost time of 【QuickSort】 is(milliseconds):0
Sort result of 【QuickSort】:{93,862,991,1285,2737,2938,3119,3372,3933,4647...}
**************************************************
Number to Sort is:25000
Array to sort is:{737677,533972,6498,772664,516805,635063,278963,284929,577222,593745...}
Cost time of 【QuickSort】 is(milliseconds):2
Sort result of 【QuickSort】:{1,247,270,375,386,428,431,515,588,623...}
**************************************************
Number to Sort is:250000
Array to sort is:{481818,650680,957183,733420,611440,739781,495686,560166,942993,492550...}
Cost time of 【QuickSort】 is(milliseconds):23
Sort result of 【QuickSort】:{0,6,10,17,20,22,23,26,32,37...}

相关代码:

 1 package com.cnblogs.riyueshiwang.sort;
 2 
 3 import java.util.Arrays;
 4 
 5 public class QuickSort extends abstractSort {
 6 
 7     @Override
 8     protected void sort(int[] toSort) {
 9         quicksort(toSort, 0, toSort.length);
10     }
11 
12     protected void quicksort(int[] array, int first, int last) {
13 
14         int pivot = array[first];
15         int i = first;
16         int j = last - 1;
17         boolean serachBig = true;
18         while (i < j) {
19             if (serachBig) {
20                 if (array[j] < pivot) {
21                     array[i] = array[j];
22                     i++;
23                     serachBig = false;
24                 } else {
25                     j--;
26                 }
27             } else {
28                 if (array[i] > pivot) {
29                     array[j] = array[i];
30                     j--;
31                     serachBig = true;
32                 } else {
33                     i++;
34                 }
35             }
36         }
37         array[i] = pivot;
38 
39         if (i - first > 1) {
40             quicksort(array, first, i);
41         }
42         if (last - i > 2) {
43             quicksort(array, i + 1, last);
44         }
45     }
46 
47     public static void main(String[] args) {
48         for (int j = 0, n = 2500; j < 3; j++, n = n * 10) {
49             System.out
50                     .println("**************************************************");
51             System.out.println("Number to Sort is:" + n);
52             int[] array = CommonUtils.getRandomIntArray(n, 1000000);
53             System.out.print("Array to sort is:");
54             CommonUtils.printIntArray(array);
55 
56             int[] array1 = Arrays.copyOf(array, n);
57             new QuickSort().sortAndprint(array1);
58         }
59     }
60 
61 }
QuickSort.java