Skip to content
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

Added Bin Sort [Go] #537

Merged
merged 6 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa

| Algorithm | C | CPP | Java | Python | Golang | JavaScript | C# |
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|:-----------------:| :-----------------:|
| [Bin Sort](http://www.cdn.geeksforgeeks.org/bucket-sort-2/)| [:white_check_mark:](bin_sort/bin_sort.c) | |[:white_check_mark:](bin_sort/BinSort.java) | [:white_check_mark:](bin_sort/bin_sort.py) | | | |
| [Bin Sort](http://www.cdn.geeksforgeeks.org/bucket-sort-2/)| [:white_check_mark:](bin_sort/bin_sort.c) | |[:white_check_mark:](bin_sort/BinSort.java) | [:white_check_mark:](bin_sort/bin_sort.py) | [:white_check_mark:](bin_sort/bin_sort.go) | | |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [:white_check_mark:](binary_search/binary_search.c) | | [:white_check_mark:](binary_search/BinarySearch.java) | [:white_check_mark:](binary_search/binary_search.py) | [:white_check_mark:](binary_search/binary_search.go) | [:white_check_mark:](binary_search/binarySearch.js) | [:white_check_mark:](binary_search/BinarySearch.cs) |
| [Breadth First Search](https://en.wikipedia.org/wiki/Breadth-first_search) | | | [:white_check_mark:](breadth_first_search/BreadthFirstSearch.java) | | | | |
| [Coin Change Problem](http://www.algorithmist.com/index.php/Coin_Change) | [:white_check_mark:](coin_change_problem/coin_change_problem.c) | | [:white_check_mark:](coin_change_problem/CoinChangeProblem.java) | [:white_check_mark:](coin_change_problem/coin_change_problem.py) | [:white_check_mark:](coin_change_problem/coin_change_problem.go) | [:white_check_mark:](coin_change_problem/coinChangeProblem.js) | |
Expand Down
65 changes: 65 additions & 0 deletions bin_sort/bin_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"fmt"
"math/rand"
Copy link
Member

Choose a reason for hiding this comment

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

exported type Bucket should have comment or be unexported

Origin: GoLintBear, Section: golint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

"sort"
)

type Bucket []float32
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid using global variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not a global variable, it's a type alias. I made for the sake of easier understanding. I'll improve the comment to explain that.

Copy link
Member

Choose a reason for hiding this comment

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

Okay! Yes, I missed it. A comment would be good :)


// Methods required to allow sorting bucket's content
func (b Bucket) Len() int { return len(b) }
Copy link
Member

Choose a reason for hiding this comment

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

Better to use len(b) directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but we have to implement Len method, as it's required for sort.Sort().

func (b Bucket) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b Bucket) Less(i, j int) bool { return b[i] < b[j] }
Copy link
Member

Choose a reason for hiding this comment

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

Please break these definitions in multiple lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. Do I need to expand to multiple lines or these single-line definitions are good to go?

Copy link
Member

Choose a reason for hiding this comment

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

Multiple lines would be good guess.

Copy link
Member

Choose a reason for hiding this comment

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

Should it be less instead of Less? Same for Swap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sort.Sort() requires these methods to be public (starting with capital letter)


// BucketSort sorts provided slice of floats using Bucket Sort algorithm
// Time Complexity: O(n)-> Avg case and O(n^2)-> Worst Case
// Constraints: 0 <= arr[i] <= 1
func BucketSort(arr []float32) []float32 {
n := len(arr)

// Create n empty buckets, each can hold up to n elements
buckets := make([]Bucket, n)
for i := 0; i < n; i++ {
// Set length to 0, but allocate memory for n elements
buckets[i] = make([]float32, 0, n)
}

// Place all the elements into corresponding buckets
for i := 0; i < n; i++ {
bi := int(float32(n) * arr[i])
buckets[bi] = append(buckets[bi], arr[i])
}

// Sort elements in each bucket
for i := 0; i < n; i++ {
sort.Sort(buckets[i])
}

// Put sorted elements into the resulting array
sorted := make([]float32, 0, n)
for i := 0; i < n; i++ {
for j := 0; j < len(buckets[i]); j++ {
sorted = append(sorted, buckets[i][j])
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Please remove this blank line.

return sorted
}

func makeRandomNumbers(n int) []float32 {
arr := make([]float32, n)
for i := range arr {
arr[i] = rand.Float32()
}

Copy link
Member

Choose a reason for hiding this comment

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

Please remove this blank line.

return arr
}

func main() {
n := 10
arr := makeRandomNumbers(n)
sorted := BucketSort(arr)
fmt.Println(sorted)
}