【问题标题】:Sort a unordered list class in python? solution?在python中对无序列表类进行排序?解决方案?
【发布时间】:2018-03-11 02:56:25
【问题描述】:

我有一个像这样的无序列表类:

from node import Node

class UnorderedList:
    """
    Unordered list
    """

    def __init__(self):
        self.head = None

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

    def __len__(self):
        """
        returns the length of the list O(1)
        """
        return self.size()


    def add(self, item):
        """
        Add item to list
        """
        temp = Node(item)
        temp.set_next(self.head)
        self.head = temp

    def size(self):
        """
        Return size of list
        """
        current = self.head
        count = 0
        while current != None:
            count = count + 1
            current = current.get_next()

        return count

    def set(self, index, newdata):
        """
        Set node-data in list at specific index
        """
        current = self.head
        previous = None
        for i in range(index):
            previous = current
            current = current.get_next()
        if current != None:
            temp = Node(newdata)
            temp.set_next(current)
            if previous is None:
                self.head = temp
            else:
                previous.set_next(temp)
        else:
            raise("index out of range")

    def getIndex(self, item):
        """get the index of an item, assume the first one (head pointing to) 
is 0"""
        index = 0
        current = self.head
        found = False
        while current != None:
            if current.get_data() == item:
                found = True
                break
            else:
                current = current.get_next()
                index += 1
        if not found:
            index = None
        return index


    def get(self, index):
        """
        Returns node data based on index
        """
        current = self.head
        for i in range(index):
            current = current.get_next()
        if current != None:
            return current.get_data()
        else:
            raise("index out of range")

    def search(self, item):
        """
        Returns True if item found, else return False
        """
        # Här ska du returnera en bool (True/False)
        # beroende på om 'item' finns i listan
        current = self.head
        found = False
        while current != None and not found:
            if current.get_data() == item:
                found = True
            else:
                current = current.get_next()
        return found

    def print_list(self):
        """
        Prints each item in list
        """
        # Traversera listan och gör en print() på varje element
        result = "["
        node = self.head
        if node != None:
            result += str(node.data)
            node = node.next
            while node:
                result += ", " + str(node.data)
                node = node.next
        result += "]"
        return result



    def remove(self, item):
        """
        Removes item from list
        """
        current = self.head
        previous = None
        found = False
        while not found:
            if current.get_data() == item:
                found = True
            else:
                previous = current
                current = current.get_next()
        if previous == None:
            self.head = current.get_next()
        else:
            previous.set_next(current.get_next())

我想创建一个班级列表,然后我可以使用“冒泡排序”功能对列表进行排序。 我的“冒泡排序”函数如下所示:

def bubble_sort(items):
    """ Bubble sort """
    Size = items.size()
    for i in range(Size):
        for j in range(Size-1-i):
            if items.get(j) > items.get(j+1):
                tmps = items.get(j+1)
                items.set(j, items.get(j+1))
                items.set(j+1, tmps)
    return items

现在让我们创建一个列表:

// create the list
myListTwo = UnorderedList()

// Add the elements to the list
myListTwo.add(4)
myListTwo.add(50)
myListTwo.add(6)
myListTwo.add(10)
myListTwo.add(60)

//print the list :
print(myListTwo.print_list())
[60, 10, 6, 50, 4]

在这个阶段一切都很好,但问题是当我想用我的 bubble_sort 函数对列表进行排序时,我得到了这个结果:

// bubble_sort my list 
sorte = bubble_sort(myListTwo)
//print the list
print(sorte.print_list())

[10, 10, 10, 10, 60, 10, 6, 50, 4]

有什么想法吗?
谢谢/乔治

【问题讨论】:

    标签: python-3.x list sorting bubble-sort unordered


    【解决方案1】:

    因此,您的实施存在两个问题。

    第一个是冒泡排序的内部代码。改变这个

    tmps = items.get(j+1)
    

    tmps = items.get(j)
    

    查看这里了解更多关于交换Python Simple Swap Function

    第二个问题是set方法。你应该删除这个

    temp = Node(newdata)
    temp.set_next(current)
    if previous is None:
        self.head = temp
    else:
        previous.set_next(temp)
    

    你应该写这样的东西

    current.set_data(newData)
    

    (不知道你具体是怎么实现Node类的)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      相关资源
      最近更新 更多