1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package main
import "fmt"
func quickSort(arr []int, startIndex, endIndex int ) {
if startIndex >= endIndex { return } pivotIndex:= partition(arr,startIndex,endIndex) quickSort(arr,startIndex,pivotIndex-1) quickSort(arr,pivotIndex+1,endIndex) }
func partition(arr []int, startIndex, endIndex int ) int {
pivot:=arr[startIndex] mark:=startIndex
for i:=startIndex+1; i<= endIndex; i++{ if arr[i]<pivot { mark++ arr[i],arr[mark] = arr[mark], arr[i] } } arr[startIndex] = arr [mark] arr[mark] = pivot return mark }
func main() {
var arr = []int{4,4,6,5,3,2,8,1} quickSort(arr,0, len(arr)-1) fmt.Print(arr) }
|