Skip to content

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

Merged
merged 5 commits into from
Aug 23, 2018

Conversation

c252
Copy link
Member

@c252 c252 commented Aug 22, 2018

Implemented the bubble sort algorithm in nim.

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.

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]
Copy link
Member

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 @@

Copy link
Member

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:
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Aug 22, 2018
@c252
Copy link
Member Author

c252 commented Aug 23, 2018 via email

@jiegillet
Copy link
Member

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

@jiegillet jiegillet self-assigned this Aug 23, 2018
@c252
Copy link
Member Author

c252 commented Aug 23, 2018 via email

@jiegillet
Copy link
Member

Almost there, you need to fix the import lines :)

@c252
Copy link
Member Author

c252 commented Aug 23, 2018 via email

@@ -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)
Copy link
Member

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

@c252
Copy link
Member Author

c252 commented Aug 23, 2018 via email

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.

All done, yay! Sorry my last message wasn't clear and thank you for your quick fixes :)

@jiegillet jiegillet merged commit c9e1f19 into algorithm-archivists:master Aug 23, 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.

3 participants