Skip to content

Instantly share code, notes, and snippets.

@ursooperduper
Created September 9, 2014 15:56
Show Gist options
  • Save ursooperduper/17f0fbe22d10d95ac5c3 to your computer and use it in GitHub Desktop.
Save ursooperduper/17f0fbe22d10d95ac5c3 to your computer and use it in GitHub Desktop.
Simple Insertion Sort algorithm implemented with Ruby
#!/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, ">")
@Hrishikesh2900
Copy link

Insertion Sort Algorithm in Ruby from: https://atechdaily.com/posts/Insertion-Sort-Algorithm-in-Ruby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment