mirror of
https://github.com/golang/go
synced 2024-11-12 10:20:27 -07:00
sort: fix computation of maxDepth to avoid infinite loop
The current computation loops indefinitely if n > 1<<30 (for 32-bit ints). R=golang-dev, gri CC=golang-dev https://golang.org/cl/5848067
This commit is contained in:
parent
c5b45aa991
commit
c5488d4f00
@ -186,10 +186,10 @@ func quickSort(data Interface, a, b, maxDepth int) {
|
||||
// Sort sorts data.
|
||||
// The algorithm used is not guaranteed to be a stable sort.
|
||||
func Sort(data Interface) {
|
||||
// Switch to heapsort if depth of 2*ceil(lg(n)) is reached.
|
||||
// Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached.
|
||||
n := data.Len()
|
||||
maxDepth := 0
|
||||
for 1<<uint(maxDepth) < n {
|
||||
for i := n; i > 0; i >>= 1 {
|
||||
maxDepth++
|
||||
}
|
||||
maxDepth *= 2
|
||||
|
Loading…
Reference in New Issue
Block a user