Я понимаю идею алгоритма сортировки вставками и знаю, как он работает, после просмотра множества видеороликов и исследований. Однако я никогда не видел для него никакого кода, поэтому попытался написать его сам.
Мой код не работал должным образом. Я хочу понять ошибку и почему она не сработала.
Вывод кода: [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
Почему моя реализация сортировки вставками не работает? [закрыто] ⇐ Python
Программы на Python
-
Anonymous
1769782622
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)
Подробнее здесь: [url]https://stackoverflow.com/questions/79879343/why-doesn-t-my-insertion-sort-implementation-work[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия