Commit b3896462 authored by Rob Pike's avatar Rob Pike

sort: avoid overflow in pivot calculation.

thanks to snilsson@nada.kth.se for the original CL.

R=gri
CC=golang-dev, snilsson
https://golang.org/cl/3280044
parent 0dc24603
...@@ -63,7 +63,7 @@ func swapRange(data Interface, a, b, n int) { ...@@ -63,7 +63,7 @@ func swapRange(data Interface, a, b, n int) {
} }
func doPivot(data Interface, lo, hi int) (midlo, midhi int) { func doPivot(data Interface, lo, hi int) (midlo, midhi int) {
m := (lo + hi) / 2 m := lo + (hi-lo)/2 // Written like this to avoid integer overflow.
if hi-lo > 40 { if hi-lo > 40 {
// Tukey's ``Ninther,'' median of three medians of three. // Tukey's ``Ninther,'' median of three medians of three.
s := (hi - lo) / 8 s := (hi - lo) / 8
......
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