Created
September 9, 2014 15:56
-
-
Save ursooperduper/17f0fbe22d10d95ac5c3 to your computer and use it in GitHub Desktop.
Simple Insertion Sort algorithm implemented with Ruby
This file contains 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
#!/usr/bin/env ruby | |
num_arr = [31, 41, 59, 26, 41, 58] | |
puts "Original array:\n\t#{num_arr.join(",")}" | |
def insertion_sort(arr, comp) | |
(1...arr.length).each do |j| | |
key = arr[j] | |
i = j - 1 | |
while i >= 0 && arr[i].method(comp).call(key) | |
arr[i+1] = arr[i] | |
i = i - 1 | |
end | |
arr[i+1] = key | |
end | |
puts "Array after insertion sort:\n\t#{arr.join(",")}" | |
end | |
insertion_sort(num_arr, ">") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Insertion Sort Algorithm in Ruby from: https://atechdaily.com/posts/Insertion-Sort-Algorithm-in-Ruby