- package algorithms
- import "fmt"
- // 鸡尾酒
- func CocktailSort(values []int, length int) {
- if len(values) <= 1 {
- return
- }
- fmt.Println("values", values)
- bottom := 0
- top := length - 1
- swapped := true
- for swapped {
- swapped = false
- for i := bottom; i < top; i++ {
- if values[i] > values[i+1] {
- values[i], values[i+1] = values[i+1], values[i]
- swapped = true
- // } else {
- // i = i + 1
- }
- fmt.Println(i, "-b->", values)
- }
- fmt.Println("efor1:", values)
- top = top - 1
- for i := top; i > bottom; i-- {
- if values[i] < values[i-1] {
- values[i], values[i-1] = values[i-1], values[i]
- swapped = true
- // // } else {
- // i = i - 1
- }
- fmt.Println(i, "-t->", values)
- }
- fmt.Println("efor2:", values)
- bottom = bottom + 1
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2110201513871.html
来源: http://www.codesnippet.cn/detail/2110201513871.html