Skip to content

feat: add bead sorting #41

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Algorithms
* [Shell sort](https://en.wikipedia.org/wiki/Shellsort)
* [counting sort](https://en.wikipedia.org/wiki/Counting_sort)
* [radix sort](https://en.wikipedia.org/wiki/Radix_sort)
* [bead sort](https://en.wikipedia.org/wiki/Bead_sort)

#### Searching

Expand Down
53 changes: 53 additions & 0 deletions sorting/bead_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"sync"
)

/*
* Bead sort - https://en.wikipedia.org/wiki/Bead_sort
*/
func BeadSort(list []int) {
const bead = 'o'
max := 1000

all := make([]byte, max*len(list))
abacus := make([][]byte, max)
for pole, space := 0, all; pole < max; pole++ {
abacus[pole] = space[:len(list)]
space = space[len(list):]
}
var wg sync.WaitGroup
wg.Add(len(list))
for row, n := range list {
go func(row, n int) {
for pole := 0; pole < n; pole++ {
abacus[pole][row] = bead
}
wg.Done()
}(row, n)
}
wg.Wait()
wg.Add(max)
for _, pole := range abacus {
go func(pole []byte) {
top := 0
for row, space := range pole {
if space == bead {
pole[row] = 0
pole[top] = bead
top++
}
}
wg.Done()
}(pole)
}
wg.Wait()
for row := range list {
x := 0
for pole := 0; pole < max && abacus[pole][row] == bead; pole++ {
x++
}
list[len(list)-1-row] = x
}
}
5 changes: 3 additions & 2 deletions sorting/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
utils "github.com/0xAX/go-algorithms"
)

var funcs = []struct{
var funcs = []struct {
name string
f Sort
f Sort
}{
{"shell", ShellSort},
{"selection", SelectionSort},
Expand All @@ -21,6 +21,7 @@ var funcs = []struct{
{"comb", CombSort},
{"cocktail", CocktailSort},
{"bubble", BubbleSort},
{"bead", BeadSort},
}

func TestSort(t *testing.T) {
Expand Down