快速排序 golang实现
直接上代码,有问题联系我
package sort
import "testing"
func QuickSort(arr []int, begin, end int) []int {
if begin >= end {
return arr
}
tar := arr[begin]
i, j := begin, end
for i < j {
for i < j && tar <= arr[j] {
j--
}
for i < j && tar <= arr[i] {
i++
}
if i < j {
arr[i], arr[j] = arr[j], arr[i]
}
}
arr[begin], arr[i] = arr[i], arr[begin]
arr = QuickSort(arr, begin, i-1)
arr = QuickSort(arr, i+1, end)
return arr
}
func TestQuickSort(t *testing.T) {
arr := []int{1, 2, 3, 4, 5, 7, 6}
t.Log(QuickSort(arr, 0, len(arr)-1))
}
标签:begin,end,int,arr,golang,排序,快速
From: https://www.cnblogs.com/genechen/p/17103390.html