Skip to content

Conversation

kenpower
Copy link
Contributor

@kenpower kenpower commented Oct 2, 2018

No description provided.

@june128 june128 added Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) labels Oct 2, 2018
@leios
Copy link
Member

leios commented Oct 2, 2018

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.

Copy link
Member

@jiegillet jiegillet left a 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?

Copy link
Member

@jiegillet jiegillet left a 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
Copy link
Member

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.

Copy link
Contributor Author

@kenpower kenpower Oct 3, 2018

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.

Copy link
Member

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.

@jiegillet jiegillet self-assigned this Oct 3, 2018
@jiegillet
Copy link
Member

I think it's ready to merge! One last thing, you can add your name to CONTRIBUTORS.md.

@jiegillet jiegillet merged commit cade450 into algorithm-archivists:master Oct 4, 2018
@jiegillet
Copy link
Member

Yay! Well done :)

kenpower added a commit to kenpower/algorithm-archive that referenced this pull request Oct 22, 2018
* 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
@kenpower kenpower deleted the bubble_sort_in_scala branch March 26, 2021 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)

4 participants