Anonymous user
Sorting algorithms/Quicksort: Difference between revisions
m
→{{header|Go}}
(→{{header|Go}}: added another version) |
m (→{{header|Go}}) |
||
Line 1,703:
"fmt"
"sort"
"math/rand"
)
func partition(a sort.Interface, first int, last int, pivotIndex int) int {
a.Swap(first, pivotIndex
left := first+1
right := last
for left <= right {
for left <= last && a.Less(left,
left++
}
for right >= first && a.Less(
right--
}
Line 1,722 ⟶ 1,723:
}
}
a.Swap(
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)
|