insert sort正在跳过最后一个元素

时间:2015-10-12 21:22:47

标签: python for-loop insertion-sort

此插入排序错误排序所有元素,但最后一个。这很奇怪bc我有一个相同的功能,可以通过不同的属性对ALL元素进行排序。我尝试复制,粘贴和改变工作功能,但这似乎是徒劳的。

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

2 个答案:

答案 0 :(得分:1)

如果.weightPerBar都是相同类型并且是数字(不是字符串或其他对象),它应该有效。如果重量是一个字符串,它可能会出现" 2"," 6"," 4"," 10"排序为" 6"," 4"," 2"," 10"。而不是10,6,4,2根据需要。

答案 1 :(得分:0)

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

metals.sort(key = lambda metal:metal.weightPerBar,reverse = True)