0

我的老师最近编写了一个关于如何对带有数字的列表进行排序的程序,我主要不了解布尔值语句,然后是循环中对数字进行排序的逻辑,因此在解释他所做的事情时将不胜感激。我有功课要做,排序是其中的一部分,所以我只是想理解他所做的这个例子。谢谢

    d = [8, 14, 3, 5, 2, 23] # lists

    size = len( d )      # size = number of elements in list
    unsorted = True      # what does this really mean and do?

    while unsorted :     # bassicly while true, but while what is true? what would make it false?
        unsorted = False     #? did he just change the variable to false? if so, why, and
                         # how is the "while unsorted" before statement still being met
        i = 0                    # this bassiclly begins the indency number in the list below
        while i < size-1 :       # so while the indency number is less than the list 
                                 # element size it will loop through the rest
            if d[i] > d[i+1] :   # if the number in d[i]is greater than the number after
                temp = d[i]      # then variable temp gets assigned that number in d[i]
                d[i] = d[i+1]    # this confuses me. whats the purpose of setting d[i] to d[i+1]?
                d[i+1] = temp    # i think this has to do with the statement above, what does it
                unsorted = True  # why is this suddenly turned back to true?


            i += 1        # adds 1 to to indency or i until it reaches the list size to stop loop
    print d

输出最终成为下面的排序列表

[2、3、5、8、14、23]

谢谢

4

2 回答 2

1

这是一个冒泡排序。冒泡排序的概念基本上是在列表末尾交换更大的数字。布尔变量用于跟踪列表是否已排序。
如果我们检查每个数字并且我们不必交换任何数字,我们就知道列表是排序的。(这基本上就是代码所做的,也是我们需要布尔变量的原因)


unsorted = True # what does this really mean and do?
这会跟踪列表是否已排序。当这是False时,我们完成了排序,我们可以print列出列表。但是,如果是True,我们必须检查列表并将数字交换到正确的位置。


while unsorted : # bassicly while true, but while what is true? what would make it false?
正如我所提到的,while True:意味着我们上次检查时列表没有排序,所以我们必须再次检查列表(即在while循环中运行代码。)


unsorted = False
这可能是棘手的部分。我们只是假设列表已排序,除非我们必须交换数字。(下面的代码是进行交换的一段代码)

if d[i] > d[i+1] :   
    temp = d[i]    # store the larger number in a temporary variable   
    d[i] = d[i+1]  # put the smaller number in the spot of the larger number
    d[i+1] = temp  # put the larger number after the smaller number
    unsorted = True # we swapped a number, so this list might not be completely sorted


于 2013-09-28T22:41:41.243 回答
1

这就是冒泡排序算法。为了按升序对数组的所有元素进行排序,该算法比较两个相邻元素,如果元素i的后继i+1具有较小的值,则交换它们的位置。

现在让我们评论您的一些评论;-)

unsorted = True      # what does this really mean and do?

声明并初始化您的布尔值。如果为 False,您将无法进入以下 while 循环。

while unsorted :     # bassicly while true, but while what is true? what would make it false?
    unsorted = False     #? did he just change the variable to false? if so, why, and
                     # how is the "while unsorted" before statement still being met

只有在进入新的“回合”之前检查执行 while 循环的条件。请检查 while 循环是如何工作的,这是一个基本结构!该变量unsorted设置为,False以便程序能够在数组完全排序后离开循环。

i = 0  # this bassiclly begins the indency number in the list below

是的,确实 Python 使用基于零的索引(您应该查找的另一个术语)。这意味着数组中的第一个元素的索引为零

while i < size-1 :  # so while the indency number is less than the list 
                # element size it will loop through the rest

这使您能够遍历数组的所有元素。但请注意,这条线可能会引发错误。真的应该是:

while i < size-2

size-1 是长度数组中最后一个元素的索引size。但是由于您总是比较一个元素及其后继元素,因此您不必检查数组的最后一个元素(它没有后继元素)。

   temp = d[i]      # then variable temp gets assigned that number in d[i]
   d[i] = d[i+1]    # this confuses me. whats the purpose of setting d[i] to d[i+1]?
   d[i+1] = temp    # i think this has to do with the statement above, what does it

这就是我告诉你的交换。元素d[i]d[i+1]开关位置。为此,您需要一个temp用于一个变量的 orary 存储。

unsorted = True  # why is this suddenly turned back to true?

因为他不得不改变数组中元素的顺序。仅当不再需要交换并且数组元素已排序时,才应允许程序离开外部 while 循环。

于 2013-09-28T22:29:50.047 回答