Skip to content

Bogosort in lua #413

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 6 commits into from
Oct 3, 2018
Merged

Conversation

Gorzoid
Copy link
Contributor

@Gorzoid Gorzoid commented Oct 1, 2018

Not sure if I need to add labels myself. Here's bogosort anyway.

@leios leios added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 1, 2018
Wasn't running
Same as bubble sort.
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! I have a few things for you to change

@@ -31,6 +31,8 @@ In code, it looks something like this:
[import:17-20, lang:"haskell"](code/haskell/bogoSort.hs)
{% sample lang="m" %}
[import:21-28, lang:"matlab"](code/matlab/bogosort.m)
{% sample lang="lua" %}
[import:1-29, lang="lua"](code/lua/bogosort.lua)
Copy link
Member

Choose a reason for hiding this comment

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

This import is for the bogosort function only, you can import the whole file in the Example Code section below

@@ -0,0 +1,29 @@

Copy link
Member

Choose a reason for hiding this comment

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

Don't open with a white line

@@ -0,0 +1,29 @@

local function shuffle(arr)
for i = 1, #arr do
Copy link
Member

Choose a reason for hiding this comment

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

I think i = 1, #arr-1 is enough


local function shuffle(arr)
for i = 1, #arr do
local rand = math.random(#arr)
Copy link
Member

Choose a reason for hiding this comment

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

Assuming you are implementing the Fisher–Yates shuffle, you should only pick numbers that are between i and #arr included.

end
end

local arr = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}
Copy link
Member

Choose a reason for hiding this comment

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

Does it ever finish with 10 elements? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do not underestimate the speed of lua, only takes 5 seconds on my slow laptop.

Copy link
Member

Choose a reason for hiding this comment

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

Hahaha fair enough :D


bogosort(arr)

print(("Sorted array: {%s}"):format(table.concat(arr,", ")))
Copy link
Member

Choose a reason for hiding this comment

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

Add a newline

@jiegillet jiegillet self-assigned this Oct 2, 2018
bogo_sort.md corrected, properly implemented Fisher–Yates shuffle
@Gorzoid
Copy link
Contributor Author

Gorzoid commented Oct 2, 2018

Think I addressed all the problems listed.

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.

Almost ready to merge!

@@ -73,6 +75,8 @@ We are done here!
[import, lang:"haskell"](code/haskell/bogoSort.hs)
{% sample lang="m" %}
[import, lang:"matlab"](code/matlab/bogosort.m)
{% sample lang="lua" %}
[import:1-29, lang="lua"](code/lua/bogosort.lua)
Copy link
Member

Choose a reason for hiding this comment

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

Here again, you don't need to write lines explicitly to import the whole file.

Line numbers not needed when importing entire file
@jiegillet jiegillet merged commit 67c2713 into algorithm-archivists:master Oct 3, 2018
kenpower pushed a commit to kenpower/algorithm-archive that referenced this pull request Oct 22, 2018
* Added Bogosort

* Fixed code

Wasn't running

* Added tests

Same as bubble sort.

* Adding entry to bogo_sort.md

* Addressing changes

bogo_sort.md corrected, properly implemented Fisher–Yates shuffle

* Remove unnecessary line specification

Line numbers not needed when importing entire file
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