布尔值和排序列表逻辑。编程简介

时间:2013-09-28 21:39:41

标签: python-2.x

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

    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]

由于

2 个答案:

答案 0 :(得分:1)

这是Bubble sort排序算法。为了按升序对数组的所有元素进行排序,此算法会比较两个相邻元素,并在元素 i 的后继 i + 1 具有较小值时交换它们的位置。

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

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

这个declares and initializes你的布尔值。如果为False,您将无法在循环时输入以下内容。

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个存储空间。

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

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

答案 1 :(得分: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