Last active
April 21, 2017 10:34
-
-
Save ryanveroniwooff/61858d07ba9b7f14b003a616ca34a43a to your computer and use it in GitHub Desktop.
Array Sorting Method in Ruby (one liner included for funsies)
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
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