こんにちは私はかなりの日の間、この選択ソートを実装するのに苦労しています。私のコードはそれに近いと感じていますが、なぜそれが得られないのか分かりません。ここで Selectionsortが正しい結果を出力していませんPython
これは、結果はこれの代わりに[4, 2, 1, 3, 5]
を取得していますです
def selectionSort(aList):
#For each index in the list...
for i in range(len(aList)):
#Assume first that current item is already correct...
minIndex = i
#For each index from i to the end...
for j in range(i + 1, len(aList)):
if aList[j] >= aList[j - 1]:
break
aList[j], aList[j - 1] = aList[j - 1], aList[j]
minIndex = aList.index(aList[j - 1])
#Save the current minimum value since we're about
#to delete it
minValue = aList[minIndex]
#Delete the minimum value from its current index
del aList[minIndex]
#Insert the minimum value at its new index
aList.insert(i, minValue)
#Return the resultant list
return aList
コメントと私のコードです:事前にあなたの助けを
[1, 2, 3, 4, 5]
おかげ
ソートしているリスト内のアイテムを削除して追加しないでください。それらを交換してください。 – kindall
ヒント:Ken Thompson(オリジナルのUnixのデザイナーと開発)は、printステートメントを自分のコードに入れてデバッグをたくさん行っていたことを、ずっと前から読んでいると思います。誰かがもっと言う必要がありますか? –