Add go quick sort
This commit is contained in:
parent
7c54ea8a0f
commit
a0838fd182
40
go/quick_sort.go
Normal file
40
go/quick_sort.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var list = []int{1, 55, 3, 6, 4, 95, 23, 57, 2, 8, 5}
|
||||
|
||||
func QuickSort(list []int) []int {
|
||||
i := list[len(list)-1]
|
||||
|
||||
left := make([]int, 0)
|
||||
right := make([]int, 0)
|
||||
|
||||
for j := 0; j < len(list)-1; j++ {
|
||||
if list[j] < i {
|
||||
left = append(left, list[j])
|
||||
} else {
|
||||
right = append(right, list[j])
|
||||
}
|
||||
}
|
||||
|
||||
if len(left) > 1 {
|
||||
QuickSort(left)
|
||||
}
|
||||
if len(right) > 1 {
|
||||
QuickSort(right)
|
||||
}
|
||||
|
||||
copy(list, left)
|
||||
copy(list[len(left):], []int{i})
|
||||
copy(list[len(left)+1:], right)
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(list)
|
||||
fmt.Println(QuickSort(list))
|
||||
}
|
Loading…
Reference in New Issue
Block a user