Created
April 3, 2019 11:10
-
-
Save omedale/d661d0ccb4b962b33a22eac9725afe52 to your computer and use it in GitHub Desktop.
Ruby Implementation of 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
| def insertion_sort(array) | |
| return array if array.size <= 1 | |
| (array.size).times do |j| | |
| while j > 0 | |
| if array[j - 1] > array[j] | |
| array[j - 1], array[j] = array[j], array[j - 1] | |
| else | |
| break | |
| end | |
| j -= 1 | |
| end | |
| end | |
| array | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment