快速排序(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

无递归好吧,乐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值