Skip to content

Commit

Permalink
Update 033-insertion-sort-python.md
Browse files Browse the repository at this point in the history
  • Loading branch information
rylim authored Oct 12, 2021
1 parent 9a2c818 commit 4821e46
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion python/033-insertion-sort-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ while i < length(A)
end while
```

The general idea behind it is to iterate through the array from beginning to end and swaps each element from the current element to the beginning of the array if a "greater" prior element is found compare to the current element. It basically sorts "subsections" of the array repeatedly until the entire array is sorted.
The general idea behind it is to iterate through the array from beginning to end and swaps each element from the current element to the beginning of the array if a "greater" prior element is found compare to the current element. It basically sorts "subsections" of the array repeatedly until the entire array is sorted by "inserting" new elements in the right order in the subsection of the array.

## Python Implementation

Expand All @@ -54,4 +54,10 @@ def insertion_sort(array):
return array
```

As you can see, it closely follows the pseudocode, with some minimal code savings using the `range` function. The `swap` was implemented inline but could be implemented as a separate function as well.

## Runtime

As mentioned, the average runtime for this algorithm is `O(n^2)`. The best case is an array that is already sorted with a runtime of `O(n)` as the current element in the array will only be compared once with a prior element. The worst case is an array that is sorted in reverse as each new element will need to be compared (and swapped) to every prior element to the beginning of the array.

Thanks for reading, any comment or feedback is most welcome!

0 comments on commit 4821e46

Please sign in to comment.