How do I implement SelectionSort and InsertionSort on a linked list in Python?

user4208546

I've implemented a Linked List class and I have a selectionSort and insertionSort function created that works on regular lists. I need to get selectionSort and insertionSort to work on linked lists, but I'm not sure where to even start, if I'm being honest.

Here's my linked list class:

class Node:
    def __init__(self, initdata):
        self.data = initdata
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self, newnext):
        self.next = newnext

class unorderedList:
    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None

    def add(self, item):
        temp = Node(item)
        temp.setNext(self.head)
        self.head = temp

    def length(self):
        current = self.head
        count = 0
        while current != None:
            count = count + 1
            current = current.getNext()

        return count

    def search(self, item):
        current = self.head
        found = False
        while current != None and not found:
            if current.getData() == item:
                found = True
            else:
                current = current.getNext()

        return found

    def remove(self, item):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()

        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext()

Here's my code for selectionSort and insertionSort. They work just fine on regular lists, but I'm having a lot of trouble figuring out where to start to get them to work on a linkedlist (unordered list).

def selectionSort(alist):
    for fillslot in range(len(alist)-1,0,-1):
            positionOfMax = 0
            for location in range(1,fillslot+1):
                    if alist[location]>alist[positionOfMax]:
                            positionOfMax = location

            temp = alist[fillslot]
            alist[fillslot] = alist[positionOfMax]
            alist[positionOfMax] = temp

def insertionSort(alist):
    for index in range(1,len(alist)):

            currentvalue = alist[index]
            position = index

            while position>0 and alist[position-1]>currentvalue:
                    alist[position] = alist[position-1]
                    position = position-1

            alist[position] = currentvalue

Any suggestions/hints would be greatly appreciated.

user4179775

I tried to implement insertionSort, The code is readable. SelectionSort should be similar, try to implement it.

def insertionSort(h):
    if h == None:
        return None
    #Make the first node the start of the sorted list.
    sortedList= h
    h=h.next
    sortedList.next= None
    while h != None:
        curr= h
        h=h.next
        if curr.data<sortedList.data:
            #Advance the nodes
            curr.next= sortedList
            sortedList= curr
        else: 
            #Search list for correct position of current.
            search= sortedList
            while search.next!= None and curr.data > search.next.data:
                search= search.next
            #current goes after search.
            curr.next= search.next
            search.next= curr
    return sortedList

def printList(d):
    s=''
    while d:
        s+=str(d.data)+"->"
        d=d.next
    print s[:-2]

l= unorderedList()
l.add(10)
l.add(12)
l.add(1)
l.add(4)
h= l.head
printList(h)

result= insertionSort(l.head)
d= result
printList(d)

Output:

4->1->12->10
1->4->10->12

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I reverse through a linked list

From Dev

Rust: How to implement linked list?

From Dev

How do I code a function to test if a linked list is sorted

From Dev

How to implement circular linked list in java?

From Dev

How do I get insertion sort to work with a doubly linked list

From Dev

How do I add an object to a linked list in alphabetical order in Java?

From Dev

How do I implement generics <E> in a linked List?

From Dev

How to implement an addition method of linked list?

From Dev

How to implement insert method in a doubly linked list?

From Dev

How do I print the next element in a linked list to a CSV file?

From Dev

How to implement linked list with 1 million nodes?

From Dev

How can I implement a linked list in java?

From Dev

How do I fix that infinite loop on a custom double linked list?

From Dev

How do I use this Linked List implementation?

From Dev

How to implement erase method in a doubly linked list

From Dev

How do I properly delete nodes of linked list in C++

From Dev

How do I remove the first node in a linked list with a remove method

From Dev

How do I Implement the Big Three Correctly: Singly Linked List (C++)

From Dev

How do I remove the only node in a linked list in java?

From Dev

How do I implement SelectionSort and InsertionSort on a linked list in Python?

From Dev

How do I get insertion sort to work with a doubly linked list

From Dev

How do I implement generics <E> in a linked List?

From Dev

How do I search through a Java Linked List

From Dev

How Do I Fix This Sorted Linked List Insertion?

From Dev

How do I properly deallocate memory allocated with a linked list?

From Dev

How can I do a recursive print using class linked list

From Dev

How do I Sort the names in a Linked List?

From Dev

How do I get the first character of a string from a linked list?

From Dev

How can I implement a len() function for linked list?

Related Related

  1. 1

    How do I reverse through a linked list

  2. 2

    Rust: How to implement linked list?

  3. 3

    How do I code a function to test if a linked list is sorted

  4. 4

    How to implement circular linked list in java?

  5. 5

    How do I get insertion sort to work with a doubly linked list

  6. 6

    How do I add an object to a linked list in alphabetical order in Java?

  7. 7

    How do I implement generics <E> in a linked List?

  8. 8

    How to implement an addition method of linked list?

  9. 9

    How to implement insert method in a doubly linked list?

  10. 10

    How do I print the next element in a linked list to a CSV file?

  11. 11

    How to implement linked list with 1 million nodes?

  12. 12

    How can I implement a linked list in java?

  13. 13

    How do I fix that infinite loop on a custom double linked list?

  14. 14

    How do I use this Linked List implementation?

  15. 15

    How to implement erase method in a doubly linked list

  16. 16

    How do I properly delete nodes of linked list in C++

  17. 17

    How do I remove the first node in a linked list with a remove method

  18. 18

    How do I Implement the Big Three Correctly: Singly Linked List (C++)

  19. 19

    How do I remove the only node in a linked list in java?

  20. 20

    How do I implement SelectionSort and InsertionSort on a linked list in Python?

  21. 21

    How do I get insertion sort to work with a doubly linked list

  22. 22

    How do I implement generics <E> in a linked List?

  23. 23

    How do I search through a Java Linked List

  24. 24

    How Do I Fix This Sorted Linked List Insertion?

  25. 25

    How do I properly deallocate memory allocated with a linked list?

  26. 26

    How can I do a recursive print using class linked list

  27. 27

    How do I Sort the names in a Linked List?

  28. 28

    How do I get the first character of a string from a linked list?

  29. 29

    How can I implement a len() function for linked list?

HotTag

Archive