Пример усовершенствованной сортировки пузырьком

The Go Playground - запустить код

package main

import "fmt"

func cocktailsort(lst []int) []int{

    n := len(lst)
    left:=0
    right:=n-1
    for {

        for i := left;i<right;i++{
            if lst[i] > lst[i+1]{
                lst[i],lst[i+1] = lst[i+1],lst[i]
            }
        }

        right -= 1

        fmt.Println(lst)

        for i := right; i > left; i-- {
            if lst[i] < lst[i - 1] {
                lst[i], lst[i - 1] = lst[i - 1], lst[i]
            }
        }

        left += 1

        fmt.Println(lst)
        fmt.Println(left,right)

        if left >= right{
            break
        }
    }

    return lst
}

func main(){
    lst := []int{5,4,1,2,0,123,1234,32,12,2345,99}
    lst = cocktailsort(lst)
}