我是靠谱客的博主 纯真树叶,最近开发中收集的这篇文章主要介绍快速排序(python,无递归,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

class QuickSort:
def __call__(self,List):
return self.main(List)
def IsOrdered(self,List):
for i in range(1,len(List)):
if List[i] < List[i - 1]:
return False
return True
def main(self,List):
subList = list()
tempList = list()
sort = InsertionSort()
subList = [-1] + tempList + [len(List)]
while not self.IsOrdered(List):
subList = [-1] + tempList + [len(List)]
subList = sort(subList)
for i in range(1,len(subList)):
#i从列表第二位开始,防止超出范围
head = subList[i - 1] + 1
end = subList[i] - 1
Len = end - head + 1
pivot = List[head]
#设pivot为列表的第一个元素
index_L = head
index_R = end
# print('n')
# # time.sleep(0.5)
# print(i)
# print('pivot', pivot)
# print("subList",subList)
# print("List",List)
# print(index_L, index_R)
if Len == 1 or Len == 0:
continue
while not index_L == index_R:
while List[index_R] >= pivot and not index_L == index_R:
#主部分
index_R -= 1
List[index_L] ,List[index_R] = List[index_R] ,List[index_L]
while List[index_L] <= pivot and not index_L == index_R:
index_L += 1
List[index_L] ,List[index_R] = List[index_R] ,List[index_L]
tempList.append(index_L)
return List

使用的辅助代码

class InsertionSort:
def __call__(self,list):
return self.main(list)
def abnormal(self,i1,i2):
return i1 > i2
#检测是否无序
def main(self,list):
for i in range(1,len(list)):
if self.abnormal(list[i-1],list[i]):
temp = list[i]
for j in range(i,-1,-1):
# print("j =",j)
if j:
if self.abnormal(list[j-1],temp):
list[j] = list[j-1]
# print(list)
continue
# print("temp", temp)
list[j] = temp
# print(list)
break
return list

无递归好吧,乐

最后

以上就是纯真树叶为你收集整理的快速排序(python,无递归的全部内容,希望文章能够帮你解决快速排序(python,无递归所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部