Skip to content

Add bogo sort in Golang #365

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

Merged
merged 4 commits into from
Oct 20, 2018
Merged

Add bogo sort in Golang #365

merged 4 commits into from
Oct 20, 2018

Conversation

christopherm99
Copy link
Contributor

This was compiled the go1.10.3 compiler on linux, and raises no errors.

@june128 june128 added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Sep 4, 2018
@leios
Copy link
Member

leios commented Sep 21, 2018

Hey, sorry it's taking so long to get to this PR. My go knolwedge isn't great and we don't have too many reviewers who use the language. Thanks for the submission!

Copy link

@Liikt Liikt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change two things. Other than that it's good code

)

func shuffle(a []int) []int {
rand.Seed(time.Now().UnixNano())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should rarely (I dare to say never) seed your prng more than once. I would move that line into the main.


func bogo_sort(a *[]int) {
for !is_sorted(*a) {
*a = shuffle(*a)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you already work with pointers why not make the shuffe inplace. So basically change *a = shuffle(*a) to shuffle(a).
I would say either remove all pointers and let every function return it's value or change line 15 to (*a)[i], (*a)[j] = (*a)[j], (*a)[i] and take in a *[]int as a parameter in shuffle.

return true
}

func bogo_sort(a *[]int) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and as a general style rule golang uses camelcase and not underlines. Meaning the function should be called bogoSort and not bogo_sort

@leios
Copy link
Member

leios commented Oct 16, 2018

Hey checking up on this PR. A review was done, but @christopherm99 has not yet updated the contents. Any further work on that?

@christopherm99
Copy link
Contributor Author

Sorry, this must have slipped my mind. I have submitted a commit which should resolve the requested changes.

func shuffle(a *[]int) {
for i := len(*a) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
(*a)[i], (*a)[j] = (*a)[j], (*a)[i]
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line still has a tab in it.

@Liikt Liikt merged commit 8871209 into algorithm-archivists:master Oct 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants