如何将步骤/过程的其他编码添加到Quicksort代码中

时间:2019-05-10 08:31:33

标签: python quicksort

以下代码需要针对此Quicksort编码的步骤/过程进行附加编码。

我似乎不知道如何添加它以显示整个Python解释器中Quicksort编码的过程。

输出:

[“鹅”,“ Y牛”,“蚂蚁”,“狗”,“驼鹿”,“母牛”,“母鸡”,“老鼠”,“青蛙”,“甲虫”]

[“蚂蚁”,“甲虫”,“母牛”,“狗”,“青蛙”,“鹅”,“母鸡”,“驼鹿”,“鼠”,“ Y牛”]

但是我想包括编码以显示输出之间的过程。

def quicksort(Item):
        print(Item)
        quickSort(Item)
        return(Item)

def quickSort(Item):
            quickSortHelper(Item,0,len(Item)-1)

def quickSortHelper(Item,first,last):
        if first<last:
            splitpoint = partition(Item,first,last)
            quickSortHelper(Item,first,splitpoint-1)
            quickSortHelper(Item,splitpoint+1,last)

def partition(Item,first,last):
        pivotvalue = Item[first]
        leftmark = first+1
        rightmark = last
        done = False
        while not done:
            while leftmark <= rightmark and Item[leftmark] <= pivotvalue:
                leftmark = leftmark + 1
            while Item[rightmark] >= pivotvalue and rightmark >= leftmark:
                rightmark = rightmark -1
            if rightmark < leftmark:
                done = True
            else:
                temp = Item[leftmark]
                Item[leftmark] = Item[rightmark]
                Item[rightmark] = temp
        temp = Item[first]
        Item[first] = Item[rightmark]
        Item[rightmark] = temp
        return(rightmark)

mylist = ["Goose","Yak","Ant","Dog","Moose","Cow","Hen","Rat","Frog","Beetle"]
print(quicksort(mylist))

1 个答案:

答案 0 :(得分:0)

添加print()的示例可能是:

def quickSortHelper(Item,first,last):
    if first<last:
        splitpoint = partition(Item,first,last)
        print(first, splitpoint, last)
        quickSortHelper(Item,first,splitpoint-1)
        quickSortHelper(Item,splitpoint+1,last)