Sorting algorithms/Quicksort: Difference between revisions

m
(→‎{{header|Go}}: added another version)
Line 1,703:
"fmt"
"sort"
"math/rand"
)
 
func partition(a sort.Interface, first int, last int, pivotIndex int) int {
a.Swap(first, pivotIndex := first) // pickmove firstit to elementbeginning
left := first+1
right := last
for left <= right {
for left <= last && a.Less(left, pivotIndexfirst) {
left++
}
for right >= first && a.Less(pivotIndexfirst, right) {
right--
}
Line 1,722 ⟶ 1,723:
}
}
a.Swap(pivotIndexfirst, right) // put pivotswap into right place
return right
}
Line 1,730 ⟶ 1,731:
return
}
pivotIndex := partition(a, first, last, rand.Intn(last - first + 1) + first)
quicksortHelper(a, first, pivotIndex-1)
quicksortHelper(a, pivotIndex+1, last)
Anonymous user