Commit c5488d4f authored by Stefan Nilsson's avatar Stefan Nilsson Committed by Robert Griesemer

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
parent c5b45aa9
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment