Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 快速排序递归性_Python_Python 3.x_Quicksort - Fatal编程技术网

Python 快速排序递归性

Python 快速排序递归性,python,python-3.x,quicksort,Python,Python 3.x,Quicksort,我正试图用python编写一个快速排序函数,但在实现它时遇到了麻烦。我理解逻辑,但是我以前从未编写过递归函数 我查阅了YouTube教程,但它们使用的逻辑通常与我的逻辑不同 由于他们有几种快速排序的方法,我将发布我的逻辑。 我的逻辑如下: 轴心是列表中随机选择的项目 轴将移动到列表中最右侧的位置 然后将列表中的每个项目与轴进行比较,以查看其是否大于轴 大于轴的第一个项目被指定为FirstBigNumber 然后再次遍历该列表。列表中第一个小于pivot交换位置的项,其FirstBigNumber

我正试图用python编写一个快速排序函数,但在实现它时遇到了麻烦。我理解逻辑,但是我以前从未编写过递归函数

我查阅了YouTube教程,但它们使用的逻辑通常与我的逻辑不同

由于他们有几种快速排序的方法,我将发布我的逻辑。 我的逻辑如下:

  • 轴心是列表中随机选择的项目
  • 轴将移动到列表中最右侧的位置
  • 然后将列表中的每个项目与轴进行比较,以查看其是否大于轴
  • 大于轴的第一个项目被指定为FirstBigNumber
  • 然后再次遍历该列表。列表中第一个小于pivot交换位置的项,其FirstBigNumber为Swapped=True
  • 然后找到一个新的FirstBigNumber
  • 重复步骤4、5、6,直到到达枢轴
  • 将pivot与FirstBigNumber交换
  • 应该对列表进行排序
  • 我的代码:

    import random
    List=[3,5,1,7,2,8]
    pivot_pos=0
    def getpivot(List):
        #Get pivot position
        pivot_pos=random.randint(0,len(List)-1)
        return pivot_pos
    
    def quicksort(List,pivot_pos):
        #Calling Get Pivot function
        getpivot(List)
        print(List)
        #Obtain pivot
        pivot=List[pivot_pos]
        print("Pivot is ",pivot)
        #Swap pivot to right of list
        List[len(List)-1],List[pivot_pos]=List[pivot_pos],List[len(List)-1]
        Swapped=True
        print(List)
        #Loop through List
        for j in range(0,len(List)-1):
            print("Checking if",List[j],"is bigger than",pivot)
            #Marks first num larger than
            if List[j]>pivot and Swapped==True:
                #FirstBigNum stores the index of the First number bigger than pivot
                FirstBigNum=List[j]
                IndexOfBigNum=j
                print("BigNum is",FirstBigNum)
                #This new Big number has not been swapped
                Swapped=False
            for i in range(0,len(List)-1):
                if List[i]<pivot:
                        #Swap the index of smaller num with first big num
                        print("Swapped",List[i]," with ",List[IndexOfBigNum])
                        List[IndexOfBigNum],List[i]=List[i],List[IndexOfBigNum]
                        print(List)
                        Swapped=True
                elif List[i]<pivot:
                    print("Pivot is bigger than",List[i])
                    #If Value greater than pivot
                    pass
                elif i == (len(List)-1):
                    #We have reached the end of the List
                    #So we need to swap the pivot with the FirstBigNum
                    List[FirstBigNum],List[i]==List[i],List[FirstBigNum]
                    print("Here")
                    print(List)
                else:
                    #Is equal to pivot
                    pass
    
    getpivot(List)
    quicksort(List,pivot_pos)
    
    我应该得到的输出是:

    [1, 2, 3, 5, 7, 8]   
    

    下面是一个使用递归和Lomuto分区的快速排序的标准实现。我包含了一个随机列表的主驱动程序,因此您可以对其进行测试:

    import random
    
    
    def quick_sort(a, l=0, r=None):
        # Recusrion requires a Base Case
        if r is None :
            r = len(a) - 1
        if l >= r :
            return 0
        # If the Base Case is not encountered then proceed to partion
        s = lomuto_partition(a, l, r)
        # call quicksort recursively
        quick_sort(a, l, s-1)
        quick_sort(a, s+1, r)
        return a
    
    
    def lomuto_partition(a, l, r):
        p = a[l]
        s = l
        for i in range(l+1, r+1) :
            if a[i] < p :
                s += 1
                a[s], a[i] = a[i], a[s]
        a[l], a[s] = a[s], a[l]
        return s
    
    
    def random_seq(n=10, lo=0, hi=100):
        seq = []
        for i in range(n):
            seq.append(random.randint(lo, hi))
        return seq
    
    
    if __name__ == '__main__' :
        n = 16
        print('n:', n)
    
        a = random_seq(n)
        print("Unsorted Random List: ", a)
    
    随机导入
    def快速排序(a,l=0,r=None):
    #重现需要一个基本情况
    如果r为无:
    r=len(a)-1
    如果l>=r:
    返回0
    #如果未遇到基本情况,则转至partion
    s=lomuto_分区(a,l,r)
    #递归调用快速排序
    快速排序(a、l、s-1)
    快速排序(a、s+1、r)
    归还
    def lomuto_分区(a、l、r):
    p=a[l]
    s=l
    对于范围内的i(l+1,r+1):
    如果a[i]

    我希望这有帮助

    您没有在任何地方使用递归,
    quicksort()
    不会在任何地方调用
    quicksort()
    getpivot()
    应该实现什么?您从不使用该函数的返回值。因此,您使用全局变量
    pivot\u pos
    ,该变量始终保持在
    0
    。您实际打印所选的轴位置,是否注意到它始终打印0?调用
    getpivot(List)
    不会修改
    pivot\u pos的全局值(无论如何都不建议修改全局变量)。您似乎对函数调用在Python中的工作方式有一个基本的误解。在尝试实施快速排序之前,也许您应该澄清您对函数的理解。接下来,您并没有实际实施快速排序。快速排序中没有“第一个大值”指针。在快速排序中,您选择一个轴,每个较小的值都会保留在对于轴,每个较大的值都移动到轴之后。因此,处理数组中的所有元素。然后将数组划分为两个子数组,并对每个子数组应用相同的算法。
    
    import random
    
    
    def quick_sort(a, l=0, r=None):
        # Recusrion requires a Base Case
        if r is None :
            r = len(a) - 1
        if l >= r :
            return 0
        # If the Base Case is not encountered then proceed to partion
        s = lomuto_partition(a, l, r)
        # call quicksort recursively
        quick_sort(a, l, s-1)
        quick_sort(a, s+1, r)
        return a
    
    
    def lomuto_partition(a, l, r):
        p = a[l]
        s = l
        for i in range(l+1, r+1) :
            if a[i] < p :
                s += 1
                a[s], a[i] = a[i], a[s]
        a[l], a[s] = a[s], a[l]
        return s
    
    
    def random_seq(n=10, lo=0, hi=100):
        seq = []
        for i in range(n):
            seq.append(random.randint(lo, hi))
        return seq
    
    
    if __name__ == '__main__' :
        n = 16
        print('n:', n)
    
        a = random_seq(n)
        print("Unsorted Random List: ", a)