简体   繁体   English

python3中的Quicksort。 最后枢轴

[英]Quicksort in python3. Last Pivot

Thanks for taking the time to read this :) I'm implementing my own version of quick-sort in python and i'm trying to get it too work within some restrictions from a previous school assignment. 感谢您抽出宝贵的时间阅读这篇文章:)我正在用python实现我自己的快速排序版本,并且我试图在以前的学校作业的限制下使其也能正常工作。 Note that the reasons I've avoided using IN is because it wasn't allowed in the project i worked on (not sure why :3). 请注意,我避免使用IN的原因是因为我从事的项目中不允许使用它(不确定为什么:3)。

it was working fine for integers and strings but i cannot manage to adapt it for my CounterList() which is a list of nodes containing an arbitrary integer and string in each even though i'm only sorting by the integers contained in those nodes. 对于整数和字符串,它工作正常,但是我无法适应它的CounterList(),这是节点列表,每个节点包含任意整数和字符串,即使我仅按这些节点中包含的整数进行排序。

Pastebins: Pastebins:


from classes_1 import CounterNode, CounterList

def bulk_append(array1, array2):
    # takes all the items in array2 and appends them to array1
    itr = 0
    array = array1
    while itr < len(array2):
        array.append(array2[itr])
        itr += 1
    return array



def quickSort(array):
    lss = CounterList()
    eql = CounterList()
    mre = CounterList()

    if len(array) <= 1:
        return array # Base case.

    else:
        pivot = array[len(array)-1].count # Pivoting on the last item.
        itr = 0
        while itr < len(array)-1: 
        # Essentially editing "for i in array:" to handle CounterLists
            if array[itr].count < pivot:
                lss.append(array[itr])
            elif array[itr].count > pivot:
                mre.append(array[itr])
            else:
                eql.append(array[itr])
            itr += 1

        # Recursive step and combining seperate lists.    
        lss = quickSort(lss)
        eql = quickSort(eql)
        mre = quickSort(mre)
        fnl = bulk_append(lss, eql)
        fnl = bulk_append(fnl, mre)
        return fnl

I know it is probably quite straightforward but i just can't seem to see the issue. 我知道这可能很简单,但是我似乎看不到这个问题。 (Pivoting on last item) (在最后一项上旋转)

Here is the test im using: 这是测试即时通讯使用:

a = CounterList()
a.append(CounterNode("ack", 11))
a.append(CounterNode("Boo", 12))
a.append(CounterNode("Cah", 9))
a.append(CounterNode("Doh", 7))
a.append(CounterNode("Eek", 5))
a.append(CounterNode("Fuu", 3))
a.append(CounterNode("qck", 1))
a.append(CounterNode("roo", 2))
a.append(CounterNode("sah", 4))
a.append(CounterNode("toh", 6))
a.append(CounterNode("yek", 8))
a.append(CounterNode("vuu", 10))
x = quickSort(a)
print("\nFinal List: \n", x)

And the resulting CounterList: 以及产生的CounterList:

['qck': 1, 'Fuu': 3, 'Eek': 5, 'Doh': 7, 'Cah': 9, 'ack': 11]

Which as you can tell, is missing multiple values? 如您所知,缺少多个值? Either way thanks for any advice, and your time. 无论哪种方式,谢谢您的建议和您的时间。

There are two mistakes in the code: 代码中有两个错误:

  1. You don't need "eql = quickSort(eql)" line because it contains all equal values, so no need to sort. 您不需要“ eql = quickSort(eql)”行,因为它包含所有相等的值,因此无需排序。

  2. In every recursive call you loose pivot (reason for missing entries) as you don't append it to any list. 在每个递归调用中,由于没有将其附加到任何列表中,因此会松开枢轴(缺少条目的原因)。 You need to append it to eql. 您需要将其附加到eql。 So after the code line shown below: 所以下面的代码行如下所示:

    pivot = array[len(array)-1].count ivot = array [len(array)-1] .count

insert this line: 插入这一行:

eql.append(array[len(array)-1])

Also remove the below line from your code as it may cause recursion depth sometimes (only with arrays with some repeating values if any repeated value selected as pivot): 还要从代码中删除以下行,因为这有时可能会导致递归深度(仅对于具有某些重复值的数组,如果选择任何重复值作为枢轴):

eql = quickSort(eql)

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

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