2番目の数字から始まる配列をソートしようとしています。前の数字が大きいかどうかを調べる前にその配列を探します。そうであれば、番号を交換したい、そうでない場合は番号を入れておきます。現在、私のコードはそれをしません。私が変更した唯一のものの下に配列を入力すると、2が11になり、真ん中に2つの11が与えられます。何がうまくいかないのですか?pythonスワップソートで正しい出力が得られない
#given an array of digits a of length N
a = [7, 3, 11, 2, 6, 16]
N = len(a)
# moving forward along a starting from the second position to the end
# define _sillysort(a, start_pos):
# set position = start_pos
# moving backwards along a from start_pos:
# if the a[position-1] is greater than a[position]:
# swap a[position-1] and a[position]
def sillysort(a, start_pos):
a_sorted = []
start_pos = a[1]
for position in a:
if a[start_pos-1] >= a[start_pos]:
a[start_pos-1], a[start_pos] = a[start_pos], a[start_pos-1]
else:
a[start_pos-1] = a[start_pos]
a_sorted.append(position)
position += 1
return a_sorted
私はこれを実行すると、sillysort(N)、私はこの出力を得る[7、3、11、11、6、16]。あなたはすでに、なぜあなたが機能でそれを再初期化している、あなたの関数の引数としてstart_posを提供している場合は