插入排序正在跳过最后一个元素

2024-10-01 09:39:04 发布

您现在位置:Python中文网/ 问答频道 /正文

这个插入排序错误地排序了除最后一个元素之外的所有元素。这很奇怪,因为我有一个相同的函数,用不同的属性对所有元素进行排序。我试着复制、粘贴和修改工作函数,但那似乎是徒劳的。你知道吗

for i in range(1, len(metals)):
   index = i
   while index != 0 and metals[index].weightPerBar > metals[index - 1].weightPerBar:
       metals[index], metals[index - 1] = metals[index - 1], metals[index]
       index -= 1

谢谢

以下是模块的其余部分:

    class Metal(struct):
        """
        Represents a single metal type, composed of:
        :slot name (str): The name of the metal
        :slot totalBars (int): The total number of bars
        :slot weightPerBar (int): The weight of a single bar
        :slot valuePerBar (int): The value of a single bar
        :slot valuePerWeight (float): The value per weight of the metal
        :slot barsTaken (int): The number of bars added to the satchel
        """
        _slots = ((str, "name"), (int, "totalBars"), (int, "weightPerBar"), (int, "valuePerBar"), (float, "valuePerWeight"), (int, "barsTaken"))

        pass


    def createMetal(name, totalBars, weightPerBar, valuePerBar):
        """
        Create and return a new Metal object.
        :param name (str): The name of the metal
        :param totalBars (int): The total number of bars
        :param weightPerBar (int): The weight of a single bar
        :param valuePerBar (int): The value of a single bar
        :return: A newly initialized Metal object
        :rtype: Metal
        """

        new_metal = Metal(name, totalBars, weightPerBar, valuePerBar)
        return new_metal

        pass

    def readMetals(fileName):
        """
        Read the metals from a file whose format is:
            metalName totalBars weightPerBar valuePerBar
        :param fileName (str): The name of the file
        :return: A list of Metal objects
        :rtype: list
        """
        metal_list = []
        file = open(fileName)
        for line in file:
            line = line.split()
            weight_per_bar = float(line[3])/float(line[2]) # creating derived value
            new_metal = Metal(line[0], int(line[1]), int(line[2]), int(line[3]), weight_per_bar, 0)

            metal_list += [new_metal]
        return metal_list

        pass


    def sortMetalsByValuePerBar(metals):
        """
        Sort the metals by value per bar using insertion sort.  The list of
        metals is modified in place to be ordered by value per bar.
        :param metals (list of Metal): The list of metals
        :return: None
        :rtype: NoneType
        """

        for i in range(1, len(metals)):
            index = i
            while index != 0 and metals[index].valuePerBar > metals[index - 1].valuePerBar:
                metals[index], metals[index - 1] = metals[index - 1], metals[index]
                index -= 1


        pass

    def sortMetalsByValuePerWeight(metals):
        """
        Sort the metals by value per weight using insertion sort.  The list of
        metals is modified in place to be ordered by value per weight.
        :param metals (list of Metal): The list of metals
        :return: None
        :rtype: NoneType
        """
        for i in range(1, len(metals)):
            index = i
            while index != 0 and metals[index].weightPerBar > metals[index - 1].weightPerBar:
                metals[index], metals[index - 1] = metals[index - 1], metals[index]
                index -= 1
        pass

Tags: ofthenameindexvaluelinebarlist
2条回答

你的代码在我的机器上运行得很好,但是为什么你要自己实现一个排序算法呢?您可以使用:

你知道吗金属.分类(键=λ金属:金属重量杆,反向=真)

如果.weightPerBar都是同一类型并且是数字(而不是字符串或其他对象),那么它应该可以工作。如果weight是一个字符串,它可能会有这样的情况,“2”、“6”、“4”、“10”排序为“6”、“4”、“2”、“10”。而不是10,6,4,2。你知道吗

相关问题 更多 >