简体   繁体   English

快速排序函数在被调用时返回 None

[英]Quicksort function returning None when being called

I don't have the best grasp on how quicksort actually works, but I've been reading through other posts on stack overflow and watching videos to understand it.我对快速排序的实际工作方式没有最好的了解,但我一直在阅读有关堆栈溢出的其他帖子并观看视频以了解它。 However, I was given some starter code a basic quicksort function and was tasked with the creating several lists both sorted and unsorted to be used with quicksort.但是,我得到了一些入门代码一个基本的快速排序功能,并负责创建几个已排序和未排序的列表,以便与快速排序一起使用。 However, I'm confused on what's actually being returned from this quicksort function.但是,我对这个快速排序函数实际返回的内容感到困惑。 You don't personally have to explain the quicksort function to me - I'll work it out on my own, but I'd love help understanding how to actually call it.您不必亲自向我解释快速排序功能 - 我会自己解决,但我很乐意帮助您了解如何实际调用它。

from random import *

def quicksort( myList, first, last ):
    """ Sort the given list using the quicksort algorithm. Sorts the
        portion of the list between the first and last indices (inclusive).
    """
    # base case: done when the indices touch or overlap.
    if first >= last: return

    # recursive case: partition the myList and recurse on both sides
    split = partition( myList, first, last )
    quicksort( myList, first, split-1 )
    quicksort( myList, split+1, last )

def partition( myList, first, last ):
    """ Partition the given list into two parts. Smaller and larger values of pivot btwn them
        Partitions the portion of the list between the first and last indices (inclusive).
        Return the index of the pivot element.
    """

    lastSmall = first

    # Separate the list into "pivot, smalls, lastSmall, larges".
    for i in range( first+1, last+1 ): # first+1 ... last (inclusive)
        # if myList[i] is small, swap it onto the 'small' side.
        if myList[ i ] <= myList[ first ]:
            lastSmall = lastSmall + 1
            swap( myList, lastSmall, i )

    # Swap the pivot with lastSmall to get "smalls, pivot, larges".
    swap( myList, first, lastSmall )

    # Return the location of the pivot
    return lastSmall

def swap( myList, first, second ):
    """ Swap the items at the first and second indices in the given list.
        Assumes the indices are legal and occupied in the list.
    """
    tmp = myList[ first ]
    myList[ first ] = myList[ second ]
    myList[ second ] = tmp

def main():

    ''' task 1 '''
    # randomized list
    pt1 = [randint(0,10) for x in range(10)]
    print(pt1) # print randomized list before quicksort
    pt1 = quicksort(pt1,0,len(task1pt1)-1)   # this keeps giving me None
    print(pt1) # print pt1 list after quicksort
    pt2 = [x for x in range(10)]
    print(pt2)


main()

If I already have a list of numbers I want to be sorted, how am I actually supposed to call quicksort?如果我已经有一个我想要排序的数字列表,我实际上应该如何调用快速排序? Based on what I remember from class, my professor told us to use 0 as first and len(myList)-1 as last, but I don't understand what quicksort returns - is it a list?根据我在课堂上的记忆,我的教授告诉我们首先使用 0,最后使用 len(myList)-1,但我不明白快速排序返回的是什么——它是一个列表吗? a singular number?单数?

The quicksort function does not return the sorted list, but it returns None by default. quicksort 函数不返回排序后的列表,但默认返回None However, it sorts the list by reference .但是,它按引用对列表进行排序。 The line pt1 = quicksort(...) is reassigning pt1 to None after it has been sorted. pt1 = quicksort(...)行在排序后将pt1重新分配给 None 。 Thus the following would work:因此,以下将起作用:

pt1 = [randint(0,10) for x in range(10)]
print(pt1) 
quicksort(pt1,0,len(pt1)-1)   
print(pt1) # prints the sorted list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM