recursive function and merge sort on doubly linked list

Update: 12-17   got someone to explain a few things to me while I was trying to get quick sort to work on the doubly linked list....  I have to redo the merge sort.   The whole node needs to be merged/copied, not just the data/value.  For example... what if the node had more in it then just a numerical value?  then when you sorted it, that data would be lost.  Buger off logical know it all mudder son's a nut bogs.   No , but really.  Had no idea.  So, once I get the swap() method figured out for my doubly linked list, and get that into my quick sort and functioning, I'll come back and fix this all up too.   I guess this still works to show someone how to do it theoretically. 

    < Tired of the gremlins lurking in my machine... I swear they sneak up...  push buttons ... and type stuff in when I'm not looking. > 

So I finally got my merge sort for my class done.  I stumbled for four days on this.  The codes I kept looking up that used the 'higher' level languages were using recursive functions.... problem was.... I didn't know that's what those were.

So I'm staring at these going.... where are all the pieces going?

 A merge sort breaks apart a list of data into it's single components... then pairs them up in thier ordered sets,  then pairs the ordered sets up.... over and over until you have a complete set that is fully ordered.   Sorted out.   Well,  the recursive function does that ,  all behind a curtain.  For a beginner like me, not knowing what was going on, I just couldn't see it.  What was this magic?  Where was all the data going?  Who had it?  How in the world was it being sorted correctly?

It looked like it was just taking the split list, and sorting that.  That's not how merge sort is supposed to work!!!!

I tried to explain it best I could.  I might be wrong.  As always,  drop a comment if you want to contribute, or make my explanation better! 

Here's a merge sort, on the doubly linked list I made in python.  The doubly linked list can be found in earlier posts.
I fixed an error in my 'shift' method in my DLList.  Found it when I was testing this.

from ex16DLL import*
from random import randint

#max_numbers = 30

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

 
def split(numbers):
    print("split >>>>>>>>>>>>>>>>>>>")
    if numbers.count() == 0 or numbers.count == 1:
        return numbers
 
    else:
        left = DLList()
        right = DLList()
        length = numbers.count()
        # use //  to get a whole number approximate half of the list
        mid = length//2
        node = numbers.begin
        count = 0
        while node:
     
            if mid <= count:
                # add the first lower count to left
                new = node.value
                left.push(new)

            else:
                new = node.value
                # add the stuff above mid to the right
                right.push(new)
             
            count += 1

            node = node.next
     
        return left, right

def merge(left, right):

    # a sorted list of size determined by left and right
    sorted = DLList()
    # if left == None....  sometimes you may be given only one item and a None
    if left == None:
        return right
    if right == None:
        return left
    # we go through the nodes, and compare values
    node1 = left.begin
    node2 = right.begin

    while node1 != None or node2 != None:
     
        if node1 == None and node2 != None:
            # if there is only one list left, they are ordered by now, and can be added
            data = node2.value
            # put the data on the list
            sorted.push(data)
           # move to the next node2 or None
            node2 = node2.next

        elif node2 ==None and node1 != None:
            data = node1.value
            sorted.push(data)
            node1 = node1.next

        elif node1.value < node2.value:
            data = node1.value
            sorted.push(data)
            #left.remove(data)
            node1 = node1.next

        else:
            data = node2.value
            sorted.push(data)
            node2 = node2.next

# me testing how the recursive is working 
    print("---merging---")
    sorted.graphical()
    return sorted

##  merge_sort is a 'recursive function' ##
def merge_sort(numbers):
    length = numbers.count()

    ### this if statement is the 'termination condition'  of my 
    ## recursive function.  DO not think of it as a normal if:
    if length == 0 or length == 1:
        # this return.... will be the end result of the final merge
        # we've reached the base case.....
        return numbers

    ## this block will run until we get all the pieces split into one
    else:
        ###  THIS FRIGGGEN PART!!!   #####
        #  we split the numbers up....
        x, y = split(numbers)
        #  we tell it to merge sort the numbers will be split until it is 1... 
        # it will split until the termination condition is met.....
        x = merge_sort(x) # (x,x,x,x, .....)  once the spam entered into if is == 1  
        y = merge_sort(y) # (y, y, y, y, y, y)  once y going in == 1
        #once these hit 1 length... they start merging  the IF  above 'terminating'

        # this return is executed when all calls to the function are executed.
        # since I have n^ number of x and y's  where they are the number of items in the
        # original list... it will merge until they are all in the list
        #  the merge will merge all x's and y's are gone?
     
        return merge(x, y)

         
 
numbers = random_list(7)
numbers.graphical()
numbers = merge_sort(numbers)








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