四、快速排序

来源:互联网 发布:离线地图数据库开发 编辑:程序博客网 时间:2024/05/29 10:59

    • 序言
        • 图解排序过程
      • 快排code
      • code地址
      • 引用

序言

快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。
步骤为:
从数列中挑出一个元素,称为”基准”(pivot),
重新排序数列,所有比基准值小的元素摆放在基准前面,所有比基准值大的元素摆在基准后面(相同的数可以到任一边)。在这个分区结束之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。
递归地(recursively)把小于基准值元素的子数列和大于基准值元素的子数列排序。
递归到最底部时,数列的大小是零或一,也就是已经排序好了。这个算法一定会结束,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。

图解排序过程:

实例分析

举例来说,现有数组 arr = [3,7,8,5,2,1,9,5,4],分区可以分解成以下步骤:
1.首先选定一个基准元素,这里我们元素 5 为基准元素(基准元素可以任意选择):

          pivot            ↓3   7   8   5   2   1   9   5   4

将基准元素与数组中最后一个元素交换位置,如果选择最后一个元素为基准元素可以省略该步:

                              pivot                                ↓3   7   8   4   2   1   9   5   5

从左到右(除了最后的基准元素),循环移动小于基准元素 5 的所有元素到数组开头,留下大于等于基准元素的元素接在后面。在这个过程它也为基准元素找寻最后摆放的位置。循环流程如下:
循环 i == 0 时,storeIndex == 0,找到一个小于基准元素的元素 3,那么将其与 storeIndex 所在位置的元素交换位置,这里是 3 自身,交换后将 storeIndex 自增 1,storeIndex == 1:

                                pivot                                  ↓  3   7   8   4   2   1   9   5   5  ↑storeIndex

循环 i == 3 时,storeIndex == 1,找到一个小于基准元素的元素 4:

     ┌───────┐                 pivot     ↓       ↓                   ↓ 3   7   8   4   2   1   9   5   5     ↑       ↑storeIndex   i

交换位置后,storeIndex 自增 1,storeIndex == 2:

                              pivot                                ↓3   4   8   7   2   1   9   5   5        ↑              storeIndex

循环 i == 4 时,storeIndex == 2,找到一个小于基准元素的元素 2:

        ┌───────┐             pivot        ↓       ↓               ↓3   4   8   7   2   1   9   5   5        ↑       ↑   storeIndex   i

交换位置后,storeIndex 自增 1,storeIndex == 3:

                              pivot                                ↓3   4   2   7   8   1   9   5   5            ↑                  storeIndex

循环 i == 5 时,storeIndex == 3,找到一个小于基准元素的元素 1:

           ┌───────┐         pivot            ↓       ↓           ↓3   4   2   7   8   1   9   5   5            ↑       ↑       storeIndex   i

交换后位置后,storeIndex 自增 1,storeIndex == 4:

                              pivot                                ↓3   4   2   1   8   7   9   5   5                ↑                      storeIndex

循环 i == 7 时,storeIndex == 4,找到一个小于等于基准元素的元素 5:

                ┌───────────┐ pivot                ↓           ↓   ↓3   4   2   1   8   7   9   5   5                ↑           ↑           storeIndex       i

交换后位置后,storeIndex 自增 1,storeIndex == 5:

                             pivot                                ↓3   4   2   1   5   7   9   8   5                    ↑                          storeIndex

循环结束后交换基准元素和 storeIndex 位置的元素的位置:

                  pivot                    ↓3   4   2   1   5   5   9   8   7                    ↑                          storeIndex

那么 storeIndex 的值就是基准元素的最终位置,这样整个分区过程就完成了。
引用维基百科上的一张图片:

快排code

//找出storeIndexint partition(Element *arr, int left, int right) {    int storeIndex = left;    int pivot = arr[right]; // 直接选最右边的元素为基准元素    int i = 0;    for (i = left; i < right; i++) {        if (arr[i] < pivot) {            swap(&arr[storeIndex], &arr[i]);            storeIndex++; // 交换位置后,storeIndex 自增 1,代表下一个可能要交换的位置        }    }    swap(&arr[storeIndex], &arr[right]);// 将基准元素放置到最后的正确位置上    return storeIndex;}void quick_sort(Element *arr, int left, int right) {    if (left > right) {        return;    }    int storeIndex = partition(arr, left, right);    quick_sort(arr, left, storeIndex - 1);    quick_sort(arr, storeIndex + 1, right);}

code地址

https://github.com/HumorSmith/Alorthim/tree/master/sort

引用

http://bubkoo.com/2014/01/12/sort-algorithm/quick-sort/

0 0