parent
39b67080f7
commit
30c184002c
@ -0,0 +1,27 @@
|
||||
package tools
|
||||
//划分
|
||||
func partition(arr *[]int,left int,right int)int{
|
||||
privot:=(*arr)[right]
|
||||
i:=left-1
|
||||
for j:=left;j<right;j++{
|
||||
if (*arr)[j]<privot{
|
||||
i++
|
||||
temp:=(*arr)[i]
|
||||
(*arr)[i]=(*arr)[j]
|
||||
(*arr)[j]=temp
|
||||
}
|
||||
}
|
||||
temp:=(*arr)[i+1]
|
||||
(*arr)[i+1]=(*arr)[right]
|
||||
(*arr)[right]=temp
|
||||
return i+1
|
||||
}
|
||||
//递归
|
||||
func QuickSort(arr *[]int,left int,right int){
|
||||
if left>= right{
|
||||
return
|
||||
}
|
||||
privot:=partition(arr,left,right)
|
||||
QuickSort(arr,left,privot-1)
|
||||
QuickSort(arr,privot+1,right)
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestQuickSort(t *testing.T) {
|
||||
arr:=[]int{1,8,3,9,4,5,4,7}
|
||||
t.Log(arr)
|
||||
QuickSort(&arr,0,len(arr)-1)
|
||||
t.Log(arr)
|
||||
}
|
Loading…
Reference in new issue