Skip to content

Commit

Permalink
add bucket sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu Zhao committed May 12, 2018
1 parent c89769b commit d0f499a
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions ch06/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func main() {
// sortFunc := selectionSort
// sortFunc := selectionSort1
// sortFunc := mergeSort
sortFunc := quickSort
// sortFunc := quickSort

// compFunc := less
compFunc := more
// compFunc := more


// ### test for func merge
Expand All @@ -34,16 +34,17 @@ func main() {

arr := []int{1,2,3,6,7,5,4,0}

sortFunc(arr, compFunc)
// sortFunc(arr, compFunc)
bucketSort(arr)
fmt.Println(arr)


compFunc = less
// compFunc = less

arr = []int{1,2,3,6,7,5,4,0}
// arr = []int{1,2,3,6,7,5,4,0}

sortFunc(arr, compFunc)
fmt.Println(arr)
// sortFunc(arr, compFunc)
// fmt.Println(arr)
}

func bubbleSort(arr []int, comp func(int, int) bool) {
Expand Down Expand Up @@ -240,3 +241,24 @@ func quickSortPart(arr []int, tempArr []int, start int, end int, comp func(int,
quickSortPart(arr, tempArr, start, head - 1, comp)
quickSortPart(arr, tempArr, tail + 1, end, comp)
}

var bucketLowerBound = 0
var bucketUpperBound = 7

func bucketSort(arr []int) {
length := len(arr)
bucketRange := bucketUpperBound - bucketLowerBound + 1
tempArr := make([]int, bucketRange)

for i := 0; i < length; i ++ {
tempArr[arr[i]] += 1
}

cnt := 0
for index, item := range tempArr {
for i := 0; i < item; i ++ {
arr[cnt] = index + bucketLowerBound
cnt ++
}
}
}

0 comments on commit d0f499a

Please sign in to comment.