# encoding=utf-8 def quick_sort(alist, start, end): """快速排序""" if start >= end: return mid = alist[start] left = start right = end while left < right: while left < right and alist[right] >= mid: right -= 1 alist[left] = alist[right] while left < right and alist[left] < mid: left += 1 alist[right] = alist[left] # 退出循环后, left于right重合,即相等 alist[left] = mid quick_sort(alist, start, left-1) quick_sort(alist, left+1, end) if __name__ == ‘__main__‘: alist = [22, 1, 4, 553, 3, 212, 77] print(alist) quick_sort(alist, 0, len(alist)-1) print(alist)
时间: 2024-11-05 11:00:46