私の先生は最近、数値でリストをソートする方法についてプログラムを書きましたが、主にブール値ステートメントと、数値をソートするためのループ内のロジックを理解していないので、彼が何をしたかを説明するだけで助けていただければ幸いです。私には宿題があり、並べ替えはその一部であるため、彼が行ったこの例を理解しようとしています。ありがとう
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]
ありがとう