1
1
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:
Divlo 2021-06-29 20:41:11 +02:00
parent d45453e00a
commit cdf7ba5026
No known key found for this signature in database
GPG Key ID: 185ED2F15F104E52
3 changed files with 31 additions and 1 deletions

View File

@ -1,4 +1,4 @@
from typing import List, Any
from typing import List
import sys

View File

@ -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²) |

View File

@ -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)