如何在 Python 中的链表上实现 SelectionSort 和 InsertionSort? [英] How do I implement SelectionSort and InsertionSort on a linked list in Python?

查看:64
本文介绍了如何在 Python 中的链表上实现 SelectionSort 和 InsertionSort?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个链接列表类,并且创建了一个selectionSort和insertSort函数,该函数可以在常规列表上使用.我需要获得selectionSort和insertSort才能在链表上工作,但是如果我说实话,我不确定从哪里开始.

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.

这是我的链表类:

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

这是我的selectionSort和insertSort的代码.它们在常规列表上工作得很好,但是我在弄清楚从哪里开始让它们在链表(无序列表)上工作时遇到了很多麻烦.

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.

推荐答案

我尝试实现 insertionSort ,该代码可读. SelectionSort 应该相似,请尝试实现它.

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)

输出:

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

这篇关于如何在 Python 中的链表上实现 SelectionSort 和 InsertionSort?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆