Skip to content

Instantly share code, notes, and snippets.

@ryanveroniwooff
Last active April 21, 2017 10:34
Show Gist options
  • Save ryanveroniwooff/61858d07ba9b7f14b003a616ca34a43a to your computer and use it in GitHub Desktop.
Save ryanveroniwooff/61858d07ba9b7f14b003a616ca34a43a to your computer and use it in GitHub Desktop.
Array Sorting Method in Ruby (one liner included for funsies)
def sort(a, a_is_unsorted = true)
# (a_is_unsorted = false; (a.size - 1).times{ |i| j = i + 1; a[i], a[j], a_is_unsorted = a[j], a[i], true if a[i] > a[j]}) while a_is_unsorted
while a_is_unsorted do
a_is_unsorted = false
(a.length - 1).times{ |i|
a[i], a[i + 1], a_is_unsorted = a[i + 1], a[i], true if a[i] > a[i + 1]
}
end
a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment