用 Golang 實做快速排序 (quick sort) 快速排序是很常用的一個排序方法,下方我將會用 Golang 實做同步以及異步的快速排序。
同步 實做 func sort(list []int, center int) (complete []int) { left := []int{} right := []int{} for _, num := range list[:center] { if num <= list[center] { left = append(left, num) } else { right = append(right, num) } } if len(list) > center+1 { for _, num := range list[center+1:] { if num <= list[center] { left = append(left, num) } else { right = append(right, num) } } } if len(left) > 1 { left = sort(left, len(left)/2) } if len(right) > 1 { right = sort(right, len(right)/2) } return append(append(left, list[center]), right.