/
rnekrasov
/
Python
Обзор
Документация
Войти
/
rnekrasov
/
Python
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Sorting Algorithms/insertion_sort.py
19 строк
652 B
arifaisal123
typo corrected
12 май 2023, 16:50
12 май 2023, 16:50
df58d65
Код
Авторство
О чём код?
def insertion_sort(nums): # Start on the second element as we assume the first element is sorted for i in range(1, len(nums)): item_to_insert = nums[i] # And keep a reference of the index of the previous element j = i - 1 # Move all items of the sorted segment forward if they are larger than # the item to insert while j >= 0 and nums[j] > item_to_insert: nums[j + 1] = nums[j] j -= 1 # Insert the item nums[j + 1] = item_to_insert # Verify it works random_list_of_nums = [9, 1, 15, 28, 6] insertion_sort(random_list_of_nums) print(random_list_of_nums)