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

これがバブルソートのソートアルゴリズムです。配列のすべての要素を昇順でソートするために、このアルゴリズムは隣接する 2 つの要素を比較し、要素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 は、 length の配列の最後の要素のインデックスです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そのためには、1 つの変数の orary ストレージが必要です。

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

配列内の要素の順序を変更する必要があったためです。プログラムは、スワッピングが不要になり、配列要素がソートされた場合にのみ、外側の while ループを終了できるようにする必要があります。

于 2013-09-28T22:29:50.047 に答える
1

これがバブルソートです。バブル ソートの概念は、基本的に、リストの末尾に向かって大きな数値を入れ替えることです。Boolean 変数は、リストがソートされているかどうかを追跡するために使用されます。
すべての番号をチェックすると、リストがソートされていることがわかり、それらのいずれも交換する必要はありません。(これが基本的にコードが行うことであり、ブール変数が必要な理由です)


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 に答える