This commit is contained in:
2023-09-22 09:51:10 -04:00
commit 1af8bd1e54
32 changed files with 346400 additions and 0 deletions

View File

@ -0,0 +1,24 @@
Insertion Sort
==============
Wikipedia on [insertion sort](http://en.wikipedia.org/wiki/Insertion_sort).
![insert sort gif](http://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif)
#### Algorithm
Insertion sort is a common sorting algorithm that, while still its worst case is still O(n**2), it is much more efficient than Bubble sort on average.
According to wikipedia, when people manually sort something, like a deck of cards for example, most use a method similar to insertion sort.
The algorithm is simple - here it is in pseudocode.
```
from i = 1 to length of array:
j = i
while j > 0 and array[j-1] > array[j]:
swap array[j] and array[j-1]
j = j - 1
```
#### Implementation
Write a function `insertion_sort()` that takes an unsorted `list` and returns a sorted `list`, using the insertion sort algorithm.

View File

@ -0,0 +1,8 @@
def insertion_sort(arr):
pass
assert insertion_sort([5,19,4,1,36,99,2]) == sorted([5,19,4,1,36,99,2])
assert insertion_sort(["Greg", "Armen", "Ken"]) == sorted(["Greg", "Armen", "Ken"])