-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Bogosort in lua #413
Conversation
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! I have a few things for you to change
contents/bogo_sort/bogo_sort.md
Outdated
@@ -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) |
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.
This import is for the bogosort
function only, you can import the whole file in the Example Code section below
@@ -0,0 +1,29 @@ | |||
|
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.
Don't open with a white line
@@ -0,0 +1,29 @@ | |||
|
|||
local function shuffle(arr) | |||
for i = 1, #arr do |
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 think i = 1, #arr-1
is enough
|
||
local function shuffle(arr) | ||
for i = 1, #arr do | ||
local rand = math.random(#arr) |
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.
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} |
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.
Does it ever finish with 10 elements? :)
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.
Do not underestimate the speed of lua, only takes 5 seconds on my slow laptop.
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.
Hahaha fair enough :D
|
||
bogosort(arr) | ||
|
||
print(("Sorted array: {%s}"):format(table.concat(arr,", "))) |
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.
Add a newline
bogo_sort.md corrected, properly implemented Fisher–Yates shuffle
Think I addressed all the problems listed. |
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.
Almost ready to merge!
contents/bogo_sort/bogo_sort.md
Outdated
@@ -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) |
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.
Here again, you don't need to write lines explicitly to import the whole file.
Line numbers not needed when importing entire file
* 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
Not sure if I need to add labels myself. Here's bogosort anyway.