-
- Notifications
You must be signed in to change notification settings - Fork 359
Bubble sort in scala implementation #417
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
Bubble sort in scala implementation #417
Conversation
I don't now scala so well, so we'll leave this up to see if someone else wants to do the review. Thanks for the submission! If no one else does it, I'll do the review. I just might ask some dumb questions. |
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.
Thank you for your contributions. For the record I don't know scala, it's my first time reading it, so I take my opinion with a grain of salt, but I know Haskell so I am familiar with functional programming in general.
I addressed a few small changes, aside from that I found the code quite complicated to read, especially the way your functions split the list into sorted and unsorted.
I know bubble sort is not well adapted for FP so there is that, but there should be a way to make a code a little more straightforward.
For example you could have a function bubbleUp(list: List[Int])
that goes through list
, drags the larger elements towards the end and returns the modified list. Call bubbleUp
n times and it's done. I do something like that in my Haskell implementation and it's 5 lines.
What do you think?
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.
Much clearer, thank you for the quick update.
I have a few more cosmetic changes to request.
| ||
def bubbleSort(list: List[Int]): List[Int] = | ||
bubbleUpLargestToTheEnd(list) match { | ||
case unsorted :+ largest => bubbleSort(unsorted) :+ largest |
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.
Just for curiosity, is the :+
operator O(n) or O(1)? I ask because in Haskell it would be O(n), which is not ideal.
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.
Thanks for your feedback @jiegillet.
Not sure but I suspect it is O(n) given how the List
is implemented. I've played around with getting rid of :+
in the pattern matcher but all my attempts are either less readable, less elegant or less idiomatic.
If I were concerned about performance, I would bring back the tail recursive approach from my first version, but that is definitely more difficult to read. Also we could implement using Array
, but then the solution starts to look like an imperative approach.
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.
I suppose we are really not concerned with performance here, so the more readable, the better.
…rithm-archive into bubble_sort_in_scala
…gorithm-archive into bubble_sort_in_scala
I think it's ready to merge! One last thing, you can add your name to |
…rithm-archive into bubble_sort_in_scala
Yay! Well done :) |
* add scala to book.json * add scala example for bubblesort * fix misaligned brace * simplfy bubble sort implemtation * fix line numbers in md * fix line numbers in md * add name to contributors
No description provided.