Created
November 1, 2016 02:09
-
-
Save sunny0425/bdbab353a32fcbcfdfae8ceb18d5627f to your computer and use it in GitHub Desktop.
冒泡排序
This file contains hidden or 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 bubble_sort(arr) | |
len = arr.length | |
(0..len).each do |i| | |
j = 0 | |
while j < len - i - 1 | |
if arr[j] > arr[j+1].to_i | |
x = arr[j] | |
arr[j] = arr[j+1] | |
arr[j+1] = x | |
end | |
j = j + 1 | |
end | |
end | |
return arr | |
end | |
bubble_sort([25,56,49,78,11,65,41,36]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment