-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Adding sorting algorithms #29
Changes from all commits
a1f09bf
3336eac
b33e5b0
5e8ef79
9df2f6f
806dbb9
9b2125a
b0f4779
40f906e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||||
## Bubble Sort | ||||||||
## | ||||||||
## https://en.wikipedia.org/wiki/Bubble_sort | ||||||||
## Time complexity: O(n^2) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Best, worst, average cases ? |
||||||||
## Auxiliary Space: O(1). | ||||||||
|
||||||||
## Import unit testing for testing purposes | ||||||||
import unittest | ||||||||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look at other implementations or the CONTRIBUTING.md file. |
||||||||
|
||||||||
## Define function, bubble_sort() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the point of this comment ? It looks redundant with the function header. |
||||||||
## Pass in array "arr" as parameter | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. Comment does not bring much. A short explanation of the |
||||||||
proc bubble_sort(arr: var openarray[int])= | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why int only? It is not less pedagogical if the type is generic.
Suggested change
|
||||||||
## Optimization: if array is already sorted, | ||||||||
## it doesn't need to do this process | ||||||||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which process? |
||||||||
var swapped = false | ||||||||
## Iterate through arr | ||||||||
for i in 0 .. high(arr) - 1: | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Nim, we have syntactic sugar to avoid all these - 1 in upper bounds.
Suggested change
|
||||||||
## high(arr) also work but outer loop will | ||||||||
## repeat 1 time more. | ||||||||
## Last i elements are already in place | ||||||||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This optimization trick is more general than this and can actually be applied at each iteration. |
||||||||
for j in 0 .. high(arr) - i - 1: | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bubble can also be increased rather than decreased.
Suggested change
|
||||||||
## Go through array from 0 to length of arr - i - 1 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pay attention, length of array is high(arr) + 1, so this is in fact: |
||||||||
## Swap if the element found is greater | ||||||||
## than the next element | ||||||||
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
if arr[j] > arr[j + 1]: | ||||||||
swap arr[j], arr[j + 1] | ||||||||
swapped = true | ||||||||
|
||||||||
if not swapped: | ||||||||
## if no need to make a single swap, just exit. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return | ||||||||
|
||||||||
## Test | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. Add a when isMainModule block here.
Suggested change
|
||||||||
test "Empty array": | ||||||||
var arr: seq[int] | ||||||||
bubble_sort(arr) | ||||||||
echo repr(arr) | ||||||||
check arr == [] | ||||||||
|
||||||||
|
||||||||
test "one element array": | ||||||||
var arr = @[1] | ||||||||
bubble_sort(arr) | ||||||||
echo repr(arr) | ||||||||
check arr == [1] | ||||||||
Comment on lines
+34
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Group them under a suite block to separate these edge cases from more interesting tests. |
||||||||
|
||||||||
|
||||||||
test "complete array": | ||||||||
var arr = @[5, 6, 2, 1, 3] | ||||||||
bubble_sort(arr) | ||||||||
echo repr(arr) | ||||||||
check arr == @[1, 2, 3, 5, 6] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work only for positive numbers? |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||||||||||||
## Insertion Sort Algorithm Implementation | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
## | ||||||||||||||||
## https://en.wikipedia.org/wiki/Insertion_sort | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put at the end. |
||||||||||||||||
## Worst Case Time Complexity: O(n^2) | ||||||||||||||||
## Best Case Time Complexity: O(n) | ||||||||||||||||
## Worst Case Space Complexity: O(n) | ||||||||||||||||
Comment on lines
+4
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are these worst/best cases corresponding to? (Sorted in order/Sorted in decreasing order)
|
||||||||||||||||
|
||||||||||||||||
## Import unit testing for testing purposes | ||||||||||||||||
import unittest | ||||||||||||||||
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. Put that under the |
||||||||||||||||
|
||||||||||||||||
## Define the function | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
proc insertion_sort(arr: var openarray[int]) = | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
## Length is the length of arr | ||||||||||||||||
var length = high(arr) | ||||||||||||||||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Iit is clearly stated in the very first Nim tutorial that:
Suggested change
or:
Suggested change
|
||||||||||||||||
|
||||||||||||||||
if length <= 1: | ||||||||||||||||
## If length is or is less than one, no execution | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your comment should not duplicate code and give algorithm insight. Avoid speaking of execution, if, while.
Suggested change
|
||||||||||||||||
return | ||||||||||||||||
|
||||||||||||||||
## Iterate through array | ||||||||||||||||
for i in 1..high(arr): | ||||||||||||||||
|
||||||||||||||||
## You can treat "key" as a temporary variable | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
var key = arr[i] | ||||||||||||||||
|
||||||||||||||||
## Move elements of arr[0..i-1], that are | ||||||||||||||||
## greater than key, to one position ahead | ||||||||||||||||
## of their current position | ||||||||||||||||
var j = i - 1 | ||||||||||||||||
while j >= 0 and key < arr[j] : | ||||||||||||||||
arr[j + 1] = arr[j] | ||||||||||||||||
j -= 1 | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another Nim's syntax sugar that I like. You can resolve this conversation if you do not like this notation.
Suggested change
|
||||||||||||||||
arr[j + 1] = key | ||||||||||||||||
|
||||||||||||||||
## Test | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above. |
||||||||||||||||
test "Empty array": | ||||||||||||||||
var arr: seq[int] | ||||||||||||||||
insertion_sort(arr) | ||||||||||||||||
echo repr(arr) | ||||||||||||||||
check arr == [] | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
test "one element array": | ||||||||||||||||
var arr = @[1] | ||||||||||||||||
insertion_sort(arr) | ||||||||||||||||
echo repr(arr) | ||||||||||||||||
check arr == [1] | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
test "complete array": | ||||||||||||||||
var arr = @[5, 6, 2, 1, 3] | ||||||||||||||||
insertion_sort(arr) | ||||||||||||||||
echo repr(arr) | ||||||||||||||||
check arr == @[1, 2, 3, 5, 6] |
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.
Sources and links should be at the end of the docstring bloc.
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.
Please, add a description of the algorithm too. Here is the one I put in my implementation.
« Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps had to be performed during a pass, meaning that the list has become fully sorted. »
Something more concise would be greatly appreciated.