-
-
Notifications
You must be signed in to change notification settings - Fork 359
Implement bubble sort in Ruby #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement bubble sort in Ruby #130
Conversation
for i in 0..arr.length - 1 | ||
for k in 0..arr.length - 2 | ||
if arr[k] > arr[k + 1] | ||
temp = arr[k + 1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a, b = b, a
notation here seems appropriate :)
Yay, Ruby code :) |
@jiegillet It looks like you know Ruby well enough to take this PR, so I assigned it to you. |
For the sake of consistency, I'd suggest to modify the code such that it is accepted by rubocop. Of course, this is a matter of preference and you might have intentionally abstained from using it. (I am also just passing by after finding this project via Youtube's 3Blue1Brown channel, so I don't know where this project stands with regard to Ruby code conventions.) The parentheses for the #!/usr/bin/env ruby
def bubble_sort(arr)
(0..arr.length - 1).each do
(0..arr.length - 2).each do |k|
next unless arr[k] > arr[k + 1]
arr[k + 1], arr[k] = arr[k], arr[k + 1]
end
end
arr
end
def main
range = [200, 79, 69, 45, 32, 5, 15, 88, 620, 125]
puts "The range before sorting is #{range}"
range = bubble_sort(range)
puts "The range after sorting is #{range}"
end
main Edit: Credit @jiegillet for the parallel assignment suggestion. |
Thanks for the input @andreaswachowski, I haven't written Ruby code in a while, so your suggestions are welcome. I also prefer |
@@ -20,6 +20,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with | |||
[import, lang:"haskell"](code/haskell/bubbleSort.hs) | |||
{% sample lang="cpp" %} | |||
[import, lang:"c_cpp"](code/c++/bubblesort.cpp) | |||
{% sample lang="rb" %} | |||
[import, lang:"ruby"](code/ruby/bubble.rb) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's skip the first two line, an merely import:3-7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be import:3-12
? So that the whole function is included.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I look again, it's import:3-15
haha. Anyway, the line count will change after more commits, the point is to just import the function :)
@jiegillet I agree with you that the |
@andreaswachowski I implemented your changes except for the next, which I did not think was necessary. |
Thank you! Unfortunately I can't even see your changes because you committed changes to 252 files :) |
8482334
to
cf776d6
Compare
Hey @jiegillet I made a forced update to my feature branch, should be up to date now (been busy at work haha) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thank you for your contribution!
No description provided.