当前位置:   article > 正文

重学数据结构与算法——快速排序

重学数据结构与算法——快速排序
def patition(li, left, right):
    count = li[left]  #record the special number
                      #it is the first number, usually
    while left < right:
        while left < right and li[right] >= count: #find the number who is less than count 
                                                   #it must judge left < right
                                                   #instead, right will minus 1 one more time
            right -= 1
        li[left] = li[right]                       #exchange left and right
        while left < right and li[left] <= count: #find the number who is greater than count 
            left += 1
        li[right] = li[left]
    li[left] = count
    return left

def quick_sort(li, left, right): #recursion
    
    if left < right:
        mid = patition(li, left, right) #compute the middle number
        quick_sort(li, left, mid - 1)   #the left recursion
        quick_sort(li, mid + 1, right)  #the right recursion
    print(li)

li = [6,3,4,9,8,1,2,5,6]
quick_sort(li, 0, len(li)-1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

写英文注释是为了锻炼自己的英语。
为大家推荐一个数据结构与算法的视频:https://www.bilibili.com/video/BV1mp4y1D7UP?p=17

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/700949
推荐阅读
相关标签
  

闽ICP备14008679号