Почему моя реализация сортировки вставками не работает? [закрыто]Python

Программы на Python
Ответить
Anonymous
 Почему моя реализация сортировки вставками не работает? [закрыто]

Сообщение Anonymous »

Я понимаю идею алгоритма сортировки вставками и знаю, как он работает, после просмотра множества видеороликов и исследований. Однако я никогда не видел для него никакого кода, поэтому попытался написать его сам.
Мой код не работал должным образом. Я хочу понять ошибку и почему она не сработала.
Вывод кода: [2, 2, 4, 4, 6, 6, 8, 8, 10, 10]
def sorting_algorithms_insertion(array):
for num in range(1,len(array)):
current_element = array[num]
# current element starts with the element in index 1 which is 2
num_before = num - 1
# if num = 1 num num_before = 0
# because insertion algorythim needs the cur element and previous element to work
# num starts from 1 to 9 from the for range len we made so num before = 1 - 1 = 0 so currnet element is 4 and num before = 0
while num_before >= 0 and array[num_before] > current_element:
# if the condition is met and the statement is true that means we update the element before to be with the same index of current element
# because for example current element = 1 and previous element = 10 that means it should go right
array[num_before + 1] = array[num_before]
# now after we moved it right and modified aka updated previous element we reset and because we made array[num_before + 1] we reset by making num before = num_before - 1 and not num_before = num - 1 because in the previous line we made
# num_before just like num and when we subtract by one it becomes the same as num_before = num - 1 so that's how we reset it
num_before = num_before - 1
# we conluded that num before = 0 so 0 >= 0 is true to the first condition is met the second condition is we get the num before aka 0 we put this 0 for the index of the array so array[num_before] is equal to array[0] for now
# so the second conditon sees is the array[0] > array[1] aka is 2 > 4? no we continue and update

current_element = array[num_before - 1]
#updating works we know current element is array[1] and array[num_before] = array[0] because the condition did not meet we went to go left and start from there so because index 1 did not satisfy the condition we want to start from index 0
# and we know index 0 of the array is array[num_before] so in conclusion we we want current element aka array[1] = to index 0 of the array aka all of this comes down to current_element = array[num_before] or array[1] = array[0]
# then we start the while loop but with current_element = array[num_before] aka array[0]

array = [2,4,6,8,10,1,3,5,7,9]
sorting_algorithms_insertion(array)
print(array)


Подробнее здесь: https://stackoverflow.com/questions/798 ... ation-work
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»