Skip to content

Instantly share code, notes, and snippets.

@taiar
Created December 19, 2019 00:55
Show Gist options
  • Save taiar/455d797c666f6503cb030d5955412b7c to your computer and use it in GitHub Desktop.
Save taiar/455d797c666f6503cb030d5955412b7c to your computer and use it in GitHub Desktop.
Prefix sorting algorithm
array = [12, 3, 12, 8, 4, 5, 25]
def sortz(arr)
sorted = arr.sort
sol = []
while sorted != sol
max = arr.max
imax = arr.index(max)
arr = (arr[0..imax].reverse + arr[(imax + 1)..-1]).reverse
sol.unshift(arr[-1])
arr = arr[0..-2]
end
sol
end
puts sortz(array).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment