基础算法 —— 快排

本博客属于原创,谢绝转载,谢谢配合

        快排是排序算法中很关键的一种,也是面试时常会被问到的类型。一般我们常见的是以数组的第一个数或者最后一个数为基数(后面会讲到),进行快排的。但是本片博客会列出所有的基本情况,供感兴趣的小伙伴参考。如有不足之处或者你发现了神马bug,请用评论砸我,非常感激~下面进入正题:

一、快排的主要思想

        快排的排序核心,个人认为在于:交换&&分治

1.1 交换

        这里先就最常见的情况(以数组的第一个数为基数,把无序数组最终按照从小到大的顺序排列)说明几点:

  • 要排序的数组,我们起名为array

  • 数组的最小位置,也就是最左端,我们叫做left;数组的最大位置,也就是最右端,我们叫做right

  • 什么是基数?
            快排的过程中,我们先从array中选取一个数,一般这个数都是low或者high对应的数(当然会有二般情况,后面我会讲到,莫方~)。我把这个数称为基数,这里我用key表示。我们想以key为分隔点,把array分成左、右两个数组。
            左边数组中的数全都比key小(左边的数组是无序的)右边的数组全都比key大(右边的数组是无序的) <这里敲黑板划重点!!没有理解这两句的小伙伴赶紧回头再看看,确保理解了>

  • 怎么让左边所有的数比key小,右边所有的数比key大呢?
            答案就是用指针找,left找左边比key大的数,right找右边比key小的数,然后把这俩数交换,小于key就会在左边,大于key的就会在右边。

1.2 分治

        分治的意思就是把一个大的问题划分成一个个小的问题,解决大问题的方法就是解决小问题的方法,

下面我们举个栗子:

以第一个数为基数,升序排列:
def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    key = array[low]

    while left < right:
        if left < right and array[left] <= key:
            left += 1
        if left < right and array[right] >= key:
            right -= 1
        if left < right:
            array[left], array[right] = array[right], array[left]

    if array[left] > key:
        left -= 1
    array[left], array[low] = array[low], array[left]
    if low == left:
        quick_sort(array, low, low)
    else:
        quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)


array = [2, 1, 4, 3, 5, 6, 7]
quick_sort(array, 0, len(array) - 1)
print(array)

以第一个数为基准数,降序排列:

def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    key = array[low]

    while left < right:
        if left < right and array[left] >= key:
            left += 1
        if left < right and array[right] <= key:
            right -= 1
        if left < right:
            array[left], array[right] = array[right], array[left]

    if array[left] < key:
        left -= 1
    array[left], array[low] = array[low], array[left]
    if low == left:
        quick_sort(array, low, low)
    else:
        quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)


array = [2, 1, 4, 3, 5, 6, 7]
quick_sort(array, 0, len(array) - 1)
print(array)

以最后一个数为基数,升序排列:

def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    key = array[high]

    while left < right:
        if left < right and array[left] <= key:
            left += 1
        if left < right and array[right] >= key:
            right -= 1
        if left < right:
            array[left], array[right] = array[right], array[left]

    if array[left] < key:
        left += 1
    array[left], array[high] = array[high], array[left]
    quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)


array = [2, 8, 4, 3, 5, 6, 7]
quick_sort(array, 0, len(array) - 1)
print(array)

以最后一个数为基准数,降序排列:

def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    key = array[high]

    while left < right:
        if left < right and array[left] >= key:
            left += 1
        if left < right and array[right] <= key:
            right -= 1
        if left < right:
            array[left], array[right] = array[right], array[left]

    if array[left] > key:
        left += 1
    array[left], array[high] = array[high], array[left]
    quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)


array = [2, 8, 4, 3, 5, 6, 7]
quick_sort(array, 0, len(array) - 1)
print(array)

以中间的数为基准数,升序排列:

def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    middle = (low + high) // 2
    key = array[middle]

    while left < right:
        if left < right and array[right] >= key:
            right -= 1
            if right == middle:
                right -= 1

        if left < right and array[left] <= key:
            left += 1
            if left == middle:
                left += 1

        if left < right:
            array[left], array[right] = array[right], array[left]

    if left < middle:
        if array[left] < key:
            left += 1
    else:
        if array[left] > key:
            left -= 1
    array[left], array[middle] = array[middle], array[left]

    quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)

以中间为基准数,降序排列:

def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    middle = (low + high) // 2
    key = array[middle]

    while left < right:
        if left < right and array[right] <= key:
            right -= 1
            if right == middle:
                right -= 1

        if left < right and array[left] >= key:
            left += 1
            if left == middle:
                left += 1

        if left < right:
            array[left], array[right] = array[right], array[left]

    if left < middle:
        if array[left] > key:
            left += 1
    else:
        if array[left] < key:
            left -= 1
    array[left], array[middle] = array[middle], array[left]

    quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)


array = [2, 8, 4, 3, 5, 6, 7]
quick_sort(array, 0, len(array) - 1)
print(array)

以任意位置为基准数,升序排列:

import random


def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    position = random.randint(left, right)
    print(position)
    key = array[position]

    while left < right:
        if left < right and array[right] >= key:
            right -= 1
            if right == position:
                right -= 1

        if left < right and array[left] <= key:
            left += 1
            if left == position:
                left += 1

        if left < right:
            array[left], array[right] = array[right], array[left]

    if left < position:
        if array[left] < key:
            left += 1
    else:
        if array[left] > key:
            left -= 1
    array[left], array[position] = array[position], array[left]

    quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)


array = [2, 8, 4, 3, 5, 6, 7]
quick_sort(array, 0, len(array) - 1)
print(array)

以任意位置为基准数,降序排列:

import random


def quick_sort(array, left, right):
    if left >= right:
        return
    low = left
    high = right
    position = random.randint(left, right)
    print(position)
    key = array[position]

    while left < right:
        if left < right and array[right] <= key:
            right -= 1
            if right == position:
                right -= 1

        if left < right and array[left] >= key:
            left += 1
            if left == position:
                left += 1

        if left < right:
            array[left], array[right] = array[right], array[left]

    if left < position:
        if array[left] > key:
            left += 1
    else:
        if array[left] < key:
            left -= 1
    array[left], array[position] = array[position], array[left]

    quick_sort(array, low, left - 1)
    quick_sort(array, left + 1, high)


array = [2, 8, 4, 3, 5, 6, 7]
quick_sort(array, 0, len(array) - 1)
print(array)

三路快排,以第一个数为基准数,升序排列:

def Qua_quick_sort(array, left, right):
    if left >= right:
        return
    index = left - 1
    low = left
    high = right
    key = array[low]
    while left < right:
        if array[left] == key:
            left += 1
        elif array[left] < key:
            index += 1
            array[index], array[left] = array[left], array[index]
            left += 1
        else:
            right -= 1
            array[left], array[right] = array[right], array[left]

    Qua_quick_sort(array, low, left - 1)
    Qua_quick_sort(array, left, high)


arr = [9, 6, 4, 2, 7, 4, 2, 1, 8, 9]

Qua_quick_sort(arr, 0, len(arr))
print(arr)

三路快排,以第一个数为基准数,降序排列:

def Qua_quick_sort(array, left, right):
    if left >= right:
        return
    index = left - 1
    low = left
    high = right
    key = array[low]
    while left < right:
        if array[left] == key:
            left += 1
        elif array[left] > key:
            index += 1
            array[index], array[left] = array[left], array[index]
            left += 1
        else:
            right -= 1
            array[left], array[right] = array[right], array[left]

    Qua_quick_sort(array, low, left - 1)
    Qua_quick_sort(array, left, high)


arr = [9, 6, 4, 2, 7, 4, 2, 1, 8, 9]

Qua_quick_sort(arr, 0, len(arr))
print(arr)

三路快排,以最后一个数为基准数,升序排列:

def Qua_quick_sort(array, left, right):
    if left >= right:
        return
    index = left - 1
    low = left
    high = right
    key = array[high - 1]
    while left < right:
        if array[left] == key:
            left += 1
        elif array[left] < key:
            index += 1
            array[index], array[left] = array[left], array[index]
            left += 1
        else:
            right -= 1
            array[left], array[right] = array[right], array[left]

    Qua_quick_sort(array, low, left - 1)
    Qua_quick_sort(array, left, high)


arr = [9, 6, 4, 2, 7, 4, 2, 1, 8, 9]

Qua_quick_sort(arr, 0, len(arr))
print(arr)

三路快排,以最后一个数为基准数,降序排列:

def Qua_quick_sort(array, left, right):
    if left >= right:
        return
    index = left - 1
    low = left
    high = right
    key = array[high - 1]
    while left < right:
        if array[left] == key:
            left += 1
        elif array[left] > key:
            index += 1
            array[index], array[left] = array[left], array[index]
            left += 1
        else:
            right -= 1
            array[left], array[right] = array[right], array[left]

    Qua_quick_sort(array, low, left - 1)
    Qua_quick_sort(array, left, high)


arr = [9, 6, 4, 2, 7, 4, 2, 1, 8, 9]

Qua_quick_sort(arr, 0, len(arr))
print(arr)

三路快排,以任意位置数为基准数,升序排列:

import random


def Qua_quick_sort(array, left, right):
    if left >= right:
        return
    index = left - 1
    low = left
    high = right
    position = random.randint(left, right - 1)
    key = array[position]
    while left < right:
        if array[left] == key:
            left += 1
        elif array[left] < key:
            index += 1
            array[index], array[left] = array[left], array[index]
            left += 1
        else:
            right -= 1
            array[left], array[right] = array[right], array[left]

    Qua_quick_sort(array, low, left - 1)
    Qua_quick_sort(array, left, high)


arr = [9, 6, 4, 2, 7, 4, 2, 1, 8, 9]

Qua_quick_sort(arr, 0, len(arr))
print(arr)

三路快排,以任意位置数为基准数,降序排列:

import random


def Qua_quick_sort(array, left, right):
    if left >= right:
        return
    index = left - 1
    low = left
    high = right
    position = random.randint(left, right - 1)
    key = array[position]
    while left < right:
        if array[left] == key:
            left += 1
        elif array[left] > key:
            index += 1
            array[index], array[left] = array[left], array[index]
            left += 1
        else:
            right -= 1
            array[left], array[right] = array[right], array[left]

    Qua_quick_sort(array, low, left - 1)
    Qua_quick_sort(array, left, high)


arr = [9, 6, 4, 2, 7, 4, 2, 1, 8, 9]

Qua_quick_sort(arr, 0, len(arr))
print(arr)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值