mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Update insert sort (#2493)
* delete duplicate file update insert sort * rename * fixed error * using enumerate()
This commit is contained in:
parent
6a395456ee
commit
43f92490fe
|
@ -1,21 +0,0 @@
|
||||||
def insertionSort(arr):
|
|
||||||
"""
|
|
||||||
>>> a = arr[:]
|
|
||||||
>>> insertionSort(a)
|
|
||||||
>>> a == sorted(a)
|
|
||||||
True
|
|
||||||
"""
|
|
||||||
for i in range(1, len(arr)):
|
|
||||||
key = arr[i]
|
|
||||||
j = i - 1
|
|
||||||
while j >= 0 and key < arr[j]:
|
|
||||||
arr[j + 1] = arr[j]
|
|
||||||
j -= 1
|
|
||||||
arr[j + 1] = key
|
|
||||||
|
|
||||||
|
|
||||||
arr = [12, 11, 13, 5, 6]
|
|
||||||
insertionSort(arr)
|
|
||||||
print("Sorted array is:")
|
|
||||||
for i in range(len(arr)):
|
|
||||||
print("%d" % arr[i])
|
|
|
@ -24,30 +24,37 @@ def insertion_sort(collection: list) -> list:
|
||||||
Examples:
|
Examples:
|
||||||
>>> insertion_sort([0, 5, 3, 2, 2])
|
>>> insertion_sort([0, 5, 3, 2, 2])
|
||||||
[0, 2, 2, 3, 5]
|
[0, 2, 2, 3, 5]
|
||||||
|
>>> insertion_sort([]) == sorted([])
|
||||||
>>> insertion_sort([])
|
True
|
||||||
[]
|
>>> insertion_sort([-2, -5, -45]) == sorted([-2, -5, -45])
|
||||||
|
True
|
||||||
>>> insertion_sort([-2, -5, -45])
|
>>> insertion_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
|
||||||
[-45, -5, -2]
|
True
|
||||||
|
>>> import random
|
||||||
|
>>> collection = random.sample(range(-50, 50), 100)
|
||||||
|
>>> insertion_sort(collection) == sorted(collection)
|
||||||
|
True
|
||||||
|
>>> import string
|
||||||
|
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
|
||||||
|
>>> insertion_sort(collection) == sorted(collection)
|
||||||
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for loop_index in range(1, len(collection)):
|
for insert_index, insert_value in enumerate(collection[1:]):
|
||||||
insertion_index = loop_index
|
temp_index = insert_index
|
||||||
while (
|
while insert_index >= 0 and insert_value < collection[insert_index]:
|
||||||
insertion_index > 0
|
collection[insert_index + 1] = collection[insert_index]
|
||||||
and collection[insertion_index - 1] > collection[insertion_index]
|
insert_index -= 1
|
||||||
):
|
if insert_index != temp_index:
|
||||||
collection[insertion_index], collection[insertion_index - 1] = (
|
collection[insert_index + 1] = insert_value
|
||||||
collection[insertion_index - 1],
|
|
||||||
collection[insertion_index],
|
|
||||||
)
|
|
||||||
insertion_index -= 1
|
|
||||||
|
|
||||||
return collection
|
return collection
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
from doctest import testmod
|
||||||
|
|
||||||
|
testmod()
|
||||||
|
|
||||||
user_input = input("Enter numbers separated by a comma:\n").strip()
|
user_input = input("Enter numbers separated by a comma:\n").strip()
|
||||||
unsorted = [int(item) for item in user_input.split(",")]
|
unsorted = [int(item) for item in user_input.split(",")]
|
||||||
print(f"{insertion_sort(unsorted) = }")
|
print(f"{insertion_sort(unsorted) = }")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user