Skip to content

Instantly share code, notes, and snippets.

View sobrinho's full-sized avatar

Gabriel Sobrinho sobrinho

View GitHub Profile
@sobrinho
sobrinho / stable_sort_example.rb
Last active May 3, 2022 23:35 — forked from cyberfox/stable_sort_example.rb
Showing unstable and stable sorting in Ruby.
# 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