stuff
This commit is contained in:
24
8-insertion-sort/README.md
Normal file
24
8-insertion-sort/README.md
Normal file
@ -0,0 +1,24 @@
|
||||
Insertion Sort
|
||||
==============
|
||||
|
||||
Wikipedia on [insertion sort](http://en.wikipedia.org/wiki/Insertion_sort).
|
||||
|
||||

|
||||
|
||||
#### 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.
|
8
8-insertion-sort/insertion_sort.py
Normal file
8
8-insertion-sort/insertion_sort.py
Normal 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"])
|
Reference in New Issue
Block a user