Last active
December 19, 2018 16:56
-
-
Save KerryJones/48ba24948671b361f68e to your computer and use it in GitHub Desktop.
Python Insertion Sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## | |
| # Insertion Sort | |
| # | |
| # Runtime complexity: O(n^2) | |
| # Space complexity: O(1) | |
| ## | |
| def insertion_sort(arr, detail = False): | |
| for i in range(1, len(arr)): | |
| j = i | |
| while arr[j] < arr[j - 1] and j > 0: | |
| arr[j], arr[j - 1] = arr[j - 1], arr[j] | |
| j -= 1 | |
| if detail: | |
| print(arr) | |
| return arr | |
| test_case = [5, 2, 1, 4, 3] | |
| print(insertion_sort(test_case)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment