golang 快速排序
golang 快速排序
package main import ( "fmt" "strconv" ) func quickSort(arr []int, low, high int) { if low < high { var pivot = partition(arr, low, high) quickSort(arr, low, pivot) quickSort(arr, pivot + 1, high) } } func partition(arr []int,low,high int) int { var pivot = arr[low] var i = low var j= high // 6 5 4 3 2 1 // 6 1 2 3 4 7 for i< j{ for arr[j] >= pivot && j > low { j-- } // for arr[i]<= pivot && i<high{ i++ } if i<j { arr[i],arr[j]=arr[j],arr[i] } } arr[low],arr[j] = arr[j],pivot // return j } func printArray(arr []int) { for i := 0; i < len(arr); i++ { fmt.Print(strconv.Itoa(arr[i]) + " ") } fmt.Println("") } func main() { var arr = []int { 15, 3, 12, 6, -9, 9, 0,8,8 } fmt.Print("Before Sorting: ") printArray(arr) quickSort(arr, 0, len(arr) - 1) fmt.Print("After Sorting: ") printArray(arr) }
参考文件:
相关推荐
田有朋 2020-04-19
Masimaro 2020-06-21
Joymine 2020-06-06
清溪算法君老号 2020-06-01
GhostLWB 2020-04-20
sunjunior 2020-04-10
shenwenjie 2020-02-26
sunjunior 2020-02-15
hanyujianke 2020-01-13
路漫 2020-01-12
sunnyJam 2019-12-31
KilluaZoldyck 2019-12-15
yuanran0 2019-12-14
Happyunlimited 2019-12-10
baike 2019-12-09
yishujixiaoxiao 2019-12-02
YUAN 2019-11-19
alicelmx 2019-11-12