Last active
September 11, 2017 19:06
-
-
Save Nitesh-Mishra/e573cb281ea439da53c43eb662ef8ed8 to your computer and use it in GitHub Desktop.
Insertion Sort program in ruby
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.rb | |
| # | |
| # $ ruby insertion_sort.rb | |
| # [1, 2, 3, 4, 5, 6, 7, 8] | |
| def insertion_sort(array) | |
| for i in 1..array.length - 1 | |
| j = i | |
| temp = array[i] | |
| while ((j>0) and (array[j-1] > temp)) | |
| array[j] = array[j-1] | |
| j -= 1 | |
| end | |
| array[j] = temp | |
| end | |
| return array | |
| end | |
| array = [6,5,3,1,8,7,2,4] | |
| insertion_sort array | |
| print array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment