快速排序

时间:2019-01-19 17:00:11   收藏:0   阅读:151

def quick_sort(sort_list, start_index, end_index):
if start_index < end_index: # 如果角标左侧小于右侧则开始排序,否则退出
  basic, i, j = sort_list[start_index], start_index, end_index
  while i < j:
    while i < j and basic <= sort_list[j]: # 基准值比j(右侧)小,那么该值不做任何事情
      j -= 1
    while i < j and basic >= sort_list[i]:
      i += 1
    sort_list[i], sort_list[j] = sort_list[j], sort_list[i]
  sort_list[i], sort_list[start_index] = sort_list[start_index], sort_list[i]
  quick_sort(sort_list, start_index, i - 1)
  quick_sort(sort_list, i + 1, end_index)
l = [10, 6, 9, 18, 20, 5, 7, 15, 14, 18,19]
quick_sort(l,0,len(l)-1)
print(l)

原文:https://www.cnblogs.com/an5456/p/10292200.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!