Skip to content

Instantly share code, notes, and snippets.

@joegiralt
Created August 12, 2015 19:25
Show Gist options
  • Save joegiralt/53e7dfdb7bb5ebdcba40 to your computer and use it in GitHub Desktop.
Save joegiralt/53e7dfdb7bb5ebdcba40 to your computer and use it in GitHub Desktop.
algo for pointing out copies (modified insertion sort)
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