Created
May 10, 2016 21:26
-
-
Save gbrl/e9fa4eca1d95056fcc9c0376bfb4998f to your computer and use it in GitHub Desktop.
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
array = [6,5,3,1,8,7,2,4] | |
pairs = [] | |
new_pairs = [] | |
newer_pairs = [] | |
# BUILD PAIRS | |
(0..array.length - 1).step(2).each do |index| | |
new_pairs << [array[index],array[index+1]] | |
end | |
# COMPARE AND SORT PAIRS | |
new_pairs.each do |pair| | |
needs_flipping = (pair[0] <=> pair[1]) | |
if needs_flipping == 1 | |
old_val = pair[0] | |
pair[0] = pair[1] | |
pair[1] = old_val | |
puts "We need to flip it. Now it's like this #{pair}." | |
end | |
new_pairs << pair | |
end | |
# COMPARE AND SORT PAIRS OF PAIRS | |
(0..new_pairs.length - 1).step(2).each do |index| | |
new_pairs.each do |pair| | |
p pair[index] | |
needs_flipping = (pair[1][0] <=> pair[0][1]) | |
if needs_flipping == 1 | |
old_val = pair[0] | |
pair[0] = pair[1] | |
pair[1] = old_val | |
puts "We need to flip it. Now it's like this #{pair}." | |
end | |
newer_pairs << pair | |
end | |
end | |
puts "Newer pairs are: #{newer_pairs}" | |
puts "****************************************" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment