Created
June 16, 2017 21:51
-
-
Save evidanary/9a5013812c70ce8d625e4556140adcdc 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
| # Insertion Sort | |
| def insertion_sort(arr) | |
| return arr if arr.nil? || arr.empty? || arr.size == 1 | |
| (1...arr.size).each do |idx| | |
| j = idx | |
| while(j>0 && arr[j] < arr[j-1]) do | |
| temp = arr[j] | |
| arr[j] = arr[j-1] | |
| arr[j-1] = temp | |
| j -= 1 | |
| end | |
| end | |
| arr | |
| end | |
| insertion_sort([3,2,1,0,9,5,4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment