İkinci sayıdan başlayacağı bir diziyi sıralamaya çalışıyorum ve önceki sayının daha büyük olup olmadığını görmek için önce ona bakıyorum. Öyleyse, sayıları değiştirmek istiyorum, aksi halde numarayı olduğu yerde tut. Şu anda benim kodum bunu yapmıyor. Diziyi girdiğim tek şeyin altında değiştirdiğim şey, 2'nin 11'e dönüşmesi, ortada iki 11'inin olması. Neyin yanlış gidiyor?python takas sırası doğru çıktı vermiyor
#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
Bu çalıştırmak, sillysort (a, K), bu çıkış elde [7, 3, 11, 11, 6, 16].