Skip to content

Instantly share code, notes, and snippets.

Avatar

Gabriel Sobrinho sobrinho

View GitHub Profile
@sobrinho
sobrinho / stable_sort_example.rb
Last active May 3, 2022 — forked from cyberfox/stable_sort_example.rb
Showing unstable and stable sorting in Ruby.
View stable_sort_example.rb
# Create a array with numbers from 1 to 100 in a random order
ary = (1..100).to_a.shuffle + (1..100).to_a.shuffle
idx = 0
paired = ary.map.with_index { |value| [value, idx += 1] }
# Now the numbers are paired; the first is the random number 1-100 the second is its sequence within the 200 entries
puts paired.inspect
# You'll see many entries with equal first values where the first of them has a higher second (sequence) number, meaning it's out of order now