Created
September 8, 2013 15:39
-
-
Save cnocon/6485728 to your computer and use it in GitHub Desktop.
Sorting method found in Chris Pine's "Learning to Program"
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 arr | |
rec_sort arr, [] | |
end | |
def rec_sort unsorted, sorted | |
if unsorted.length <= 0 | |
return sorted | |
end | |
# So if we got here, then it means we still # have work to do. | |
smallest = unsorted.pop | |
still_unsorted = [] | |
unsorted.each do |tested_object| | |
if tested_object < smallest | |
still_unsorted.push smallest | |
smallest = tested_object | |
else | |
still_unsorted.push tested_object | |
end | |
end | |
# Now "smallest" really does point to the | |
# smallest element that "unsorted" contained, | |
# and all the rest of it is in "still_unsorted". | |
sorted.push smallest | |
rec_sort still_unsorted, sorted | |
end | |
puts(sort(['can','feel','singing','like','a','can'])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment