-
Notifications
You must be signed in to change notification settings - Fork 497
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
Added Bin Sort [Go] #537
Changes from 2 commits
eaaa13b
9187657
afdd328
fc642dd
59f4868
e658a59
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,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"sort" | ||
) | ||
|
||
type Bucket []float32 | ||
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. Please avoid using global variables. 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. 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. 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. 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) } | ||
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. Better to use 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. 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] } | ||
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. Please break these definitions in multiple lines. 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. Ok. Do I need to expand to multiple lines or these single-line definitions are good to go? 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. Multiple lines would be good guess. 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. Should it be 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. 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]) | ||
} | ||
} | ||
|
||
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. Please remove this blank line. |
||
return sorted | ||
} | ||
|
||
func makeRandomNumbers(n int) []float32 { | ||
arr := make([]float32, n) | ||
for i := range arr { | ||
arr[i] = rand.Float32() | ||
} | ||
|
||
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. Please remove this blank line. |
||
return arr | ||
} | ||
|
||
func main() { | ||
n := 10 | ||
arr := makeRandomNumbers(n) | ||
sorted := BucketSort(arr) | ||
fmt.Println(sorted) | ||
} |
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.
exported type Bucket should have comment or be unexported
Origin: GoLintBear, Section:
golint
.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.
Fixed