Last active
July 11, 2019 06:02
-
-
Save DanielAmah/5ce961b4c1fbaadd5511c641f565e065 to your computer and use it in GitHub Desktop.
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
| # implementation of insertion sort | |
| # Pick an item from an array | |
| # compare all items in the sorted sub-list | |
| # shift all items in the sorted sub-list greater than the item to be sorted | |
| # insert the item | |
| # repeat until the list is sorted. | |
| def insertion_sort(arr) | |
| a_length = arr.length | |
| a_length.times do |i| | |
| while i > 0 | |
| if arr[i - 1] > arr[i] | |
| arr[i], arr[i - 1] = arr[i - 1], arr[i] | |
| else | |
| break | |
| end | |
| i -= 1 | |
| end | |
| end | |
| arr | |
| end | |
| arr = [2, 5, 6, 2, 5425, 54, 5, 4, 12, 7, 3, 5, 5] | |
| print insertion_sort(arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment