Python快速排序(采用递归的方法)

 实现思想:通过key的值来对整个列表进行分割,通过key与列表中其他所有数进行一一粗略比较(一个数比它大就放在原处,比它小就互换位置),使key左边的数都比它小,右边的数都比它大。

def sub_sort(array,low,high):  
    
    key = array[low]

    while low < high:

        while low < high and  array[high]>= key:  #注意等于号必须在这里,否则报错。因为我
                  #们是对小于key的数进行排序操作的,必须保证条件唯一性,不能带等号。

            high -= 1         #即从后往前只要这个数不小于key就不进行任何操作

        while low < high and array[high] < key:

            array[low] = array[high]      #保证每一次由上面的while产生的新的较小数都能被换
                                           #到较低的位置

            low += 1   #low+1因为与array[high]交换位置后的low位置的值已经比key小了,这时我
                        #们要将low+1处的值与key相比

            array[high] = array[low]   #因为循环条件是 array[high],所以这里要进行一个赋值 

    array[low] = key      #当low不再小于high,此时已经完成分割,因为low左边的值一定比low小,
                          #右边的一定比low大,这个位置就是我们想要的key的位置

    return low            #返回low的值作为分割线,low左右两边再进行同样的操作

def quick_sort(array,low,high):

     if low < high:
        key_index = sub_sort(array,low,high)   #接收返回值low给index,注意这个函数里面的
                                               #low和high是不变的,在上个函数里的只是它们
                                                #的局部变量

        quick_sort(array,low,key_index)  #以low到key_index区域内进行排序(此时key的值
                                         #改变为上一个key找到的第一个小于它的值)排完后程序
                                         #会再次执行到这里按照相同的方法再对剩下的数排序,递
                                         #归调用,直到初始key值左侧全部按顺序排好后。退出
                                         #s_s 进入q_s,但是此时key_index=low=0,不满足if
                                         #直接退出,一层层的退,直到退出到最初的key_index,
                                         #然后执行key值后面的循环迭代排序方法一样
                                        #由于执行到low 和 key_index都等于零的情况不满足
                                         #if条件,本次递归结束,程序向下执行

        quick_sort(array,key_index+1,high)   #注意这里key_index必须加1否则报错,因为程序会
                                             #一直满足key <= array[high]条件,并且返回的
                                             #low还是原来的key_index 程序死在这里

array = [16,30,13,19,20,25,9,15,27,18]
quick_sort(array,0,len(array)-1)
print(array)

 

下面给出打印版上述代码:

def sub_sort(array,low,high):
    print("进入SS")
    key = array[low]
    print("一 key =",key)
    print( low= key)
        while low < high and  array[high]>= key:
            high -= 1
            print("二 high =",high)
        print(low < high and array[high] < key)
        while low < high and array[high] < key:
            array[low] = array[high]
            print("三 array[low1] =", array[low], "array[high1] =", array[high],"array =",array)
            low += 1
            print("四 low =",low)
            array[high] = array[low]
            print("五  array[low2] =",array[low] , "array[high2] =" ,array[high],"array =",array)
    array[low] = key
    print("六 array[low]",array[low],"low",low)
    print(array)
    return low


def quick_sort(array,low,high):

     print("进入QS",low[16, 30, 13, 19, 20, 25, 9, 15, 27, 18]
进入QS True
进入判断
进入SS
一 key = 16
True
True
二 high = 8
二 high = 7
True
三 array[low1] = 15 array[high1] = 15 array = [15, 30, 13, 19, 20, 25, 9, 15, 27, 18]
四 low = 1
五  array[low2] = 30 array[high2] = 30 array = [15, 30, 13, 19, 20, 25, 9, 30, 27, 18]
True
二 high = 6
True
三 array[low1] = 9 array[high1] = 9 array = [15, 9, 13, 19, 20, 25, 9, 30, 27, 18]
四 low = 2
五  array[low2] = 13 array[high2] = 13 array = [15, 9, 13, 19, 20, 25, 13, 30, 27, 18]
三 array[low1] = 13 array[high1] = 13 array = [15, 9, 13, 19, 20, 25, 13, 30, 27, 18]
四 low = 3
五  array[low2] = 19 array[high2] = 19 array = [15, 9, 13, 19, 20, 25, 19, 30, 27, 18]
True
二 high = 5
二 high = 4
二 high = 3
False
六 array[low] 16 low 3
[15, 9, 13, 16, 20, 25, 19, 30, 27, 18]
七 跳出SS进入QS1 low = 0 high = 9 key_index = 3
进入QS True
进入判断
进入SS
一 key = 15
True
True
二 high = 2
True
三 array[low1] = 13 array[high1] = 13 array = [13, 9, 13, 16, 20, 25, 19, 30, 27, 18]
四 low = 1
五  array[low2] = 9 array[high2] = 9 array = [13, 9, 9, 16, 20, 25, 19, 30, 27, 18]
三 array[low1] = 9 array[high1] = 9 array = [13, 9, 9, 16, 20, 25, 19, 30, 27, 18]
四 low = 2
五  array[low2] = 9 array[high2] = 9 array = [13, 9, 9, 16, 20, 25, 19, 30, 27, 18]
六 array[low] 15 low 2
[13, 9, 15, 16, 20, 25, 19, 30, 27, 18]
七 跳出SS进入QS1 low = 0 high = 3 key_index = 2
进入QS True
进入判断
进入SS
一 key = 13
True
True
二 high = 1
True
三 array[low1] = 9 array[high1] = 9 array = [9, 9, 15, 16, 20, 25, 19, 30, 27, 18]
四 low = 1
五  array[low2] = 9 array[high2] = 9 array = [9, 9, 15, 16, 20, 25, 19, 30, 27, 18]
六 array[low] 13 low 1
[9, 13, 15, 16, 20, 25, 19, 30, 27, 18]
七 跳出SS进入QS1 low = 0 high = 2 key_index = 1
进入QS True
进入判断
进入SS
一 key = 9
True
True
二 high = 0
False
六 array[low] 9 low 0
[9, 13, 15, 16, 20, 25, 19, 30, 27, 18]
七 跳出SS进入QS1 low = 0 high = 1 key_index = 0
进入QS False
八 跳出QS1进入QS2 low = 0 high = 1 key_index = 0
进入QS False
九 跳出QS2结束本次QS low = 0 high = 1 key_index = 0
八 跳出QS1进入QS2 low = 0 high = 2 key_index = 1
进入QS False
九 跳出QS2结束本次QS low = 0 high = 2 key_index = 1
八 跳出QS1进入QS2 low = 0 high = 3 key_index = 2
进入QS False
九 跳出QS2结束本次QS low = 0 high = 3 key_index = 2
八 跳出QS1进入QS2 low = 0 high = 9 key_index = 3
进入QS True
进入判断
进入SS
一 key = 20
True
False
True
三 array[low1] = 18 array[high1] = 18 array = [9, 13, 15, 16, 18, 25, 19, 30, 27, 18]
四 low = 5
五  array[low2] = 25 array[high2] = 25 array = [9, 13, 15, 16, 18, 25, 19, 30, 27, 25]
True
二 high = 8
二 high = 7
二 high = 6
True
三 array[low1] = 19 array[high1] = 19 array = [9, 13, 15, 16, 18, 19, 19, 30, 27, 25]
四 low = 6
五  array[low2] = 19 array[high2] = 19 array = [9, 13, 15, 16, 18, 19, 19, 30, 27, 25]
六 array[low] 20 low 6
[9, 13, 15, 16, 18, 19, 20, 30, 27, 25]
七 跳出SS进入QS1 low = 4 high = 9 key_index = 6
进入QS True
进入判断
进入SS
一 key = 18
True
True
二 high = 5
二 high = 4
False
六 array[low] 18 low 4
[9, 13, 15, 16, 18, 19, 20, 30, 27, 25]
七 跳出SS进入QS1 low = 4 high = 6 key_index = 4
进入QS False
八 跳出QS1进入QS2 low = 4 high = 6 key_index = 4
进入QS True
进入判断
进入SS
一 key = 19
True
True
二 high = 5
False
六 array[low] 19 low 5
[9, 13, 15, 16, 18, 19, 20, 30, 27, 25]
七 跳出SS进入QS1 low = 5 high = 6 key_index = 5
进入QS False
八 跳出QS1进入QS2 low = 5 high = 6 key_index = 5
进入QS False
九 跳出QS2结束本次QS low = 5 high = 6 key_index = 5
九 跳出QS2结束本次QS low = 4 high = 6 key_index = 4
八 跳出QS1进入QS2 low = 4 high = 9 key_index = 6
进入QS True
进入判断
进入SS
一 key = 30
True
False
True
三 array[low1] = 25 array[high1] = 25 array = [9, 13, 15, 16, 18, 19, 20, 25, 27, 25]
四 low = 8
五  array[low2] = 27 array[high2] = 27 array = [9, 13, 15, 16, 18, 19, 20, 25, 27, 27]
三 array[low1] = 27 array[high1] = 27 array = [9, 13, 15, 16, 18, 19, 20, 25, 27, 27]
四 low = 9
五  array[low2] = 27 array[high2] = 27 array = [9, 13, 15, 16, 18, 19, 20, 25, 27, 27]
六 array[low] 30 low 9
[9, 13, 15, 16, 18, 19, 20, 25, 27, 30]
七 跳出SS进入QS1 low = 7 high = 9 key_index = 9
进入QS True
进入判断
进入SS
一 key = 25
True
True
二 high = 8
二 high = 7
False
六 array[low] 25 low 7
[9, 13, 15, 16, 18, 19, 20, 25, 27, 30]
七 跳出SS进入QS1 low = 7 high = 9 key_index = 7
进入QS False
八 跳出QS1进入QS2 low = 7 high = 9 key_index = 7
进入QS True
进入判断
进入SS
一 key = 27
True
True
二 high = 8
False
六 array[low] 27 low 8
[9, 13, 15, 16, 18, 19, 20, 25, 27, 30]
七 跳出SS进入QS1 low = 8 high = 9 key_index = 8
进入QS False
八 跳出QS1进入QS2 low = 8 high = 9 key_index = 8
进入QS False
九 跳出QS2结束本次QS low = 8 high = 9 key_index = 8
九 跳出QS2结束本次QS low = 7 high = 9 key_index = 7
八 跳出QS1进入QS2 low = 7 high = 9 key_index = 9
进入QS False
九 跳出QS2结束本次QS low = 7 high = 9 key_index = 9
九 跳出QS2结束本次QS low = 4 high = 9 key_index = 6
九 跳出QS2结束本次QS low = 0 high = 9 key_index = 3
[9, 13, 15, 16, 18, 19, 20, 25, 27, 30]

 

你可能感兴趣的:(python)