quicksort with swap nodes

She works!!!  with the swap nodes..... I still want to try it with a DLList that contains more then just a data.value....  But in theory it should work just the same......
## https://en.wikipedia.org/wiki/Quicksort ##  Watched Zeds video and got the just data version working,  then put my swap_any() in.  It worked.  I had a templist at first, but it wasn't necessary.  I'm going to tinker some more. 

It all hinged on the 'get_node'. (Zeds)  I kept trying to keep track of what node I was on while the sorting was happening...  that made it impossible to navigate through the list properly when nodes where swapped.  Jeeesum Crow... all of that. It was just that simple little thing.





from ex16DLL import*
from random import randint

def random_list(count):
    numbers = DLList()
    for i in range(count, 0, -1):
        newnumber = randint(0, 100)
        numbers.push(newnumber)
    return numbers


def get_node(alist, index):
    node = alist.begin
    count = 0
    while node:
        if index == count:
            return node
        node = node.next
        count += 1




def partition(numbers, first, last):
 
    pivot = numbers.get(last)
    ## in this one, if you dont get the last as your pivot, it will not sort right
    ## it's comparing the first values and sorting left, if you use the first value
    ## as your pivot, it won't sort all the values to the left properly, especially the first one.
    #print(pivot)
    i = first - 1
    ## i  index  
 
    for j in range(first, last):
    ## j, another index j starts at first... in the first split 0:
    ## j increments on each loop of for:
        if numbers.get(j) < pivot:  # put all the numbers less then the pivot on the left
            i = i + 1  ## index i  + 1
            if i != j:   ### if they are not at the same index in the list:
                node1 = get_node(numbers, i) 
                ## it's already been established that the i's,  the index's before J, are > or < pivot. 
                ## [ 038117]  once we get to  1,  i will be on 3... i + 1 moves it to 8.. and the swap will 
                ## trade 8 and 1.  the next j will be a 1 and and i will move +1 again to 8,  trade, until 8
                ## is at the second to last.  
                ## if 8 were, say 0... the i, would have been resting on it instead of the 3, because the if
               ##  < pivot would have executed..    therefore  0 would not 
                ## have been swapped.  because if i=j, no swap occurs. in this version anyways.
                node2 = get_node(numbers, j)
                #node1.value, node2.value = node2.value, node1.value  <-- original swap
                numbers.swap_any(node1, node2) # my swap
    if numbers.get(last) < numbers.get(i + 1):  # swap if last.value is less then index + 1
        node1 = get_node(numbers, i+1)
        node2 = get_node(numbers, last)
        #node1.value, node2.value = node2.value, node1.value
        numbers.swap_any(node1, node2)
    return i + 1  # return where the last i was at in list for the partition to continue


def myquicksort(numbers):
 
    #numbers = make_temporary_list(numbers)
    length = numbers.count() - 1
    recursor(numbers, 0, length)
 

def recursor(numbers, first, last):


    if first < last:

        split = partition(numbers, first, last)
        recursor(numbers, first, split - 1)
        recursor(numbers, split + 1, last)


















Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame