-
-
Notifications
You must be signed in to change notification settings - Fork 360
implemented bubble sort in nim #359
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
implemented bubble sort in nim #359
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.
Looks good! A few small changes will make it great ^^
if a[j+1] > a[j]: | ||
swap(a[j],a[j+1]) | ||
|
||
var x: array[10,int] = [32,32,64,16,128,8,256,4,512,2] |
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.
Print the unsorted array first, maybe add some message: "Unsorted: " "Sorted: "
@@ -0,0 +1,14 @@ | |||
|
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.
Starting with an empty line bothers me more than it probably should
@@ -0,0 +1,14 @@ | |||
|
|||
proc print_array(a: openArray[int]) = | |||
for n in 0 .. len(a)-1: |
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.
You use .. < len(a)
below. Either is fine, but consistency is better.
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 only need to do < len(a) for the j loop because otherwise it will be out of bounds. If I do that with the other two, it won't go through the whole array. Thanks for spotting that.
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.
That's not exactly what I meant.
There are to identical ways to loop for an array, either 0 .. len(a)-1
or 0 .. < len(a)
. I just thought it would look better to stick to only one notation, now you are using <
twice and -1
once. I think using either <
or -1
everywhere is better.
Unrelated issue, you are now running the loop for i
len(a)+1
times, where I think you only really need len(a)-1
. The loop for j
is good as it is now.
I think I fixed it now.
…On Wed, Aug 22, 2018 at 7:42 PM, Jie ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In contents/bubble_sort/code/nim/bubble_sort.nim
<#359 (comment)>
:
> @@ -0,0 +1,14 @@
+
+proc print_array(a: openArray[int]) =
+ for n in 0 .. len(a)-1:
That's not exactly what I meant.
There are to identical ways to loop for an array, either 0 .. len(a)-1 or 0
.. < len(a). I just thought it would look better to stick to only one
notation, now you are using < twice and -1 once. I think using either <
or -1 everywhere is better.
Unrelated issue, you are now running the loop for i len(a)+1 times, where
I think you only really need len(a)-1. The loop for j is good as it is
now.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#359 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AXqo7vlsYSwDX3q3HEojfiJ-MwIJLVMRks5uTexTgaJpZM4WG0yz>
.
|
Four more things (in increasing order of importance):
|
I think I fixed it (for real).
…On Wed, Aug 22, 2018 at 8:10 PM, Jie ***@***.***> wrote:
Four more things (in increasing order of importance):
- You don't need the parentheses in 0 .. < (len(a) - 1)
- You haven't addressed my request for adding "Unsorted: " "Sorted: "
- You are still doing one pass to many on your loop for i (If you sort
a array of two values, you only need one pass to sort it)
- You are sorting the array in descending order, the norm for sorting
algorithms is sorting in ascending order
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#359 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AXqo7sIxthyytoHADI2Pvhf8OCf6ryYNks5uTfMMgaJpZM4WG0yz>
.
|
Almost there, you need to fix the import lines :) |
Sorry for being ignorant but what do you mean by import lines?
…On Wed, Aug 22, 2018 at 8:22 PM, Jie ***@***.***> wrote:
Almost there, you need to fix the import lines :)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#359 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AXqo7tr3dbaGAqRqhMcX_DxtByglFVigks5uTfW1gaJpZM4WG0yz>
.
|
contents/bubble_sort/bubble_sort.md
Outdated
@@ -46,6 +46,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with | |||
[import:3-15, lang:"php"](code/php/bubble_sort.php) | |||
{% sample lang="lisp" %} | |||
[import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp) | |||
{% sample lang="nim" %} | |||
[import:6-10, lang:"nim"](code/nim/bubble_sort.nim) |
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.
Change 6-10
to 5-9
since you removed that empty line
Oh I see what you mean :). Thanks for being so helpful Jie, I really
appreciate it.
…On Wed, Aug 22, 2018 at 8:25 PM, Cyrus Burt ***@***.***> wrote:
Sorry for being ignorant but what do you mean by import lines?
On Wed, Aug 22, 2018 at 8:22 PM, Jie ***@***.***> wrote:
> Almost there, you need to fix the import lines :)
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <#359 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AXqo7tr3dbaGAqRqhMcX_DxtByglFVigks5uTfW1gaJpZM4WG0yz>
> .
>
|
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.
All done, yay! Sorry my last message wasn't clear and thank you for your quick fixes :)
Implemented the bubble sort algorithm in nim.