Created
August 12, 2015 19:25
-
-
Save joegiralt/53e7dfdb7bb5ebdcba40 to your computer and use it in GitHub Desktop.
algo for pointing out copies (modified insertion sort)
This file contains hidden or 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
def sort_and_find_copies(array) | |
array.each_with_index do |element, idx| | |
previous_idx = idx - 1 | |
while previous_idx >= 0 | |
puts "#{array[previous_idx]} is a copy!" if array[previous_idx] == element | |
break if array[previous_idx] <= element | |
array[previous_idx + 1] = array[previous_idx] | |
previous_idx -= 1 | |
end | |
array[previous_idx + 1] = element | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment