MapReduceFilter
MapReduceFilter

Reputation: 237

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. 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).

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.

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:

['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.

Upvotes: 0

Views: 191

Answers (1)

Irshad Bhat
Irshad Bhat

Reputation: 8709

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.

  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. So after the code line shown below:

    pivot = 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)

Upvotes: 1

Related Questions