快速排序中递归错误

时间:2019-07-13 17:21:46

标签: python algorithm quicksort

我在堆栈中看到了许多有关Python的Quicksort实现问题的疑问,但是找不到我想要的东西。我的问题特定于以下实现:

android:onClick="@={(view) -> login.onLoginButtonClick(view)}"

android:onClick="@={(view)-> register.onButtonLoginRegisterClick(view)}"

    //pass the view parameter to their respective method

因此,想法是将数组的第一个元素用作枢轴元素,并在枢轴元素的基础上进行分区。 (这里我不解释算法,因为我认为这里并不重要)

运行上面的代码后,我得到以下输出:

# Implementation of Quick-Sort algorithm in Python as taught in the course: Design and Analysis of Algorithms on Coursera

def QuickSort(array, length):
    if (length == 1 or length == 0):
        return

    numComparisons = 0
    numComparisons = numComparisons + length - 1

    print('array to be sorted is: ', array) 
    pivot = ChoosePivot(array,length)
    print('Pivot is: ', pivot)
    left    = 0
    right = length

    pivotIndex = Partition(array,left,right)

    print('array after partitioning step is: ', array)
    print('Pivot Index in the array is:', pivotIndex)

    QuickSort(array[:pivotIndex], pivotIndex)
    QuickSort(array[pivotIndex+1:], length-pivotIndex-1)
    print('Sorting is done! Final array is:', array)

    return numComparisons,array

def ChoosePivot(array,length):
    #For the first part of the question, choosing first element of array as pivot element
    return (array[0])

def Swap(array,elementLeft,elementRight):
    tmp = array[elementLeft]
    array[elementLeft] = array[elementRight]
    array[elementRight] = tmp
    return array

def Partition(array,left,right):
    pivot = array[left]
    i = left + 1
    j = left + 1    

    for j in range(right):
        if (array[j] < pivot):
            Swap(array,j,i)
            i = i + 1

    # send pivot to the correct position
    array = Swap(array,left,i-1)
    return (i-1)

array = [7,5,4,6,1,15,12]
numElements = len(array)  
numComp, array = QuickSort(array,numElements)
print('Total number of comparisons',numComp)
print(array)

因此,即使显示排序已完成,我也无法理解为什么元素的位置会变回原始元素(请查看标有'--->的行) ' 在前)。

我觉得这与传递数组的方式有关。任何帮助将不胜感激,如果需要更多详细信息,请告诉我

2 个答案:

答案 0 :(得分:1)

尝试更改:

array = Swap(array,left,i-1)

收件人:

Swap(array,left,i-1)

当您向函数内的数组分配值时,python创建一个新数组,并且您丢失了对原始数组的引用

编辑: 我认为问题是出于同样的原因对QuickSort的函数调用,将数组与开始/结束索引一起传递而不是剪切它。 另一个问题是在分区函数中,您应该将j左添加。

这是完整的代码:

def QuickSort(array, start, end, length):
    if (end - start <=1):
        return

    numComparisons = 0
    numComparisons = numComparisons + length - 1

    print('array to be sorted is: ', array[start:end]) 
    pivot = ChoosePivot(array[start:end],length)
    print('Pivot is: ', pivot)
    left    = start
    right = end

    pivotIndex = Partition(array,left,right)

    print('array after partitioning step is: ', array[start:end])
    print('Pivot Index in the array is:', pivotIndex)

    QuickSort(array,start, pivotIndex, pivotIndex)
    QuickSort(array, pivotIndex+1, end, length-pivotIndex-1)
    print('Sorting is done! Final array is:', array[start:end])

    return numComparisons,array

def ChoosePivot(array,length):
    #For the first part of the question, choosing first element of array as pivot element
    return (array[0])

def Swap(array,elementLeft,elementRight):
    tmp = array[elementLeft]
    array[elementLeft] = array[elementRight]
    array[elementRight] = tmp
    return array

def Partition(array,left,right):
    pivot = array[left]
    i = left + 1
    j = left + 1    

    for j in range(right):
        if (array[left+j] < pivot):
            Swap(array,left+j,i)
            i = i + 1

    # send pivot to the correct position
    Swap(array,left,i-1)
    return (i-1)

array = [7,5,4,6,1,15,12]
numElements = len(array)  
numComp, array = QuickSort(array,0,6,numElements)
print('Total number of comparisons',numComp)
print(array)

答案 1 :(得分:1)

简化的单功能版本:

company = soup.find_all('span', class_='company')
title = soup.find_all('div', class_='title')
for t,c  in zip(title, company): 
print ("Job_Title :%s Company_Name :%s" %(t.text,c.text))