mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add sorting-algorithms/python/insertion-sort
This commit is contained in:
parent
d45453e00a
commit
cdf7ba5026
@ -1,4 +1,4 @@
|
||||
from typing import List, Any
|
||||
from typing import List
|
||||
import sys
|
||||
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
# sorting-algorithms/python/insertion-sort
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 29 June 2021.
|
||||
|
||||
| Algorithm | Best Case | Average Case | Worst Case |
|
||||
| ----------------------------------------------------------- | ----------- | ------------ | ----------- |
|
||||
| [Insertion sort](https://wikipedia.org/wiki/Insertion_sort) | O(n) | O(n²) | O(n²) |
|
@ -0,0 +1,23 @@
|
||||
from typing import List
|
||||
import sys
|
||||
|
||||
|
||||
def insertion_sort(numbersInput: List[int]) -> List[int]:
|
||||
numbers = list(numbersInput)
|
||||
for index_1 in range(1, len(numbers)):
|
||||
current = numbers[index_1]
|
||||
index_2 = index_1 - 1
|
||||
while index_2 >= 0 and numbers[index_2] > current:
|
||||
numbers[index_2 + 1] = numbers[index_2]
|
||||
index_2 -= 1
|
||||
numbers[index_2 + 1] = current
|
||||
return numbers
|
||||
|
||||
|
||||
numbers: List[int] = []
|
||||
for value in sys.stdin:
|
||||
numbers.append(int(value.rstrip('\n')))
|
||||
|
||||
sorted_numbers = insertion_sort(numbers)
|
||||
for number in sorted_numbers:
|
||||
print(number)
|
Loading…
Reference in New Issue
Block a user