Skip to content

Lisp bogo sort #512

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 19, 2018
Merged

Lisp bogo sort #512

merged 4 commits into from
Oct 19, 2018

Conversation

Trashtalk217
Copy link
Contributor

In the spirit of bogo sort I've patched this one together rather quickly, the shuffle is slow and the sortedp could maybe be simpler. But it overal works, ship it! :)

@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 13, 2018
@Trashtalk217 Trashtalk217 changed the title First working version of bogo sort in lisp Lisp bogo sort Oct 14, 2018
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.

A couple things to fix :)

do
(rotatef
(nth i list)
(nth (random (length list)) list))
Copy link
Member

Choose a reason for hiding this comment

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

This is not correct (if you are implementing Fisher-Yates algorithm), you should pick a second index j in the range i <= j < length list .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was actually implementing the Knuth shuffle (see here) and it's really hard not to copy the code straight so this was the best I could do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you know shuffling algorithms which are faster or easier to implement in FP, please let me know. The current one is not very efficient: nth and rotatef both have O(n).
The knuth shuffle seems to be designed for arrays instead of lists.
Bogo sort is, on the other hand, not exactly made to be very efficient.

Copy link
Member

@jiegillet jiegillet Oct 17, 2018

Choose a reason for hiding this comment

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

The Knuth shuffle is another name for Fisher-Yates. You have to either loop for i from 0 to length list - 2 and pick j from i to length list - 1 (incl) OR loop for i from length list - 1 to 0 and pick j from 0 to i (incl). Right now you're doing a mix of both.

And yeah, it's not very efficient. In my Haskell implementation I transformed the list into a map, made the n swaps, and back into a list. Going through an array would possibly be better, I'm not sure.

Lists are horrible data structures in many cases, but they are simple, it's fine for learning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I got the shuffle method down, (I even found a nice shortcut for in- and decrementing by one) and I'm not going to bother using arrays. Lists are clear and easy to use and this is bogo sort after all.

"Checks if a list is sorted"
(if (< (length list) 2)
t
(if (< (first list) (second list))
Copy link
Member

Choose a reason for hiding this comment

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

This should be <= I think

@jiegillet
Copy link
Member

Also you didn't include it in the .md file

@Trashtalk217
Copy link
Contributor Author

I think I fixed it?

@jiegillet jiegillet merged commit c763d0d into algorithm-archivists:master Oct 19, 2018
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.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants