Golang语言社区

排序问题总结

2018-01-04  本文已影响2人  吃猫的鱼0

简单获取连续的数字

//传入的array必须为有序递增的
//num为获取连续几个数字
func getContinuityNumber(array []int,num int)  [][]int {
    if array==nil {
        return nil//传入数据为空
    }
    lenth:=len(array)
    var arrays [][]int
    var arri []int
    max:=array[0]-1
    for k:=0;k<lenth ;k++  {
        if array[k]!=max+1 {
            if len(arrays)>0 {//当获得一组数据时就停止,直接返回数据
                return arrays
            }
            if arri!=nil&&len(arri)>num {//将数组添加到二维数组中
                arrays=append(arrays, arri)
            }
            arri=make([]int,0)
        }
        arri=append(arri,array[k] )//
        max=array[k]//最大值编程元素本身
    }
    return arrays
}

利用goroutine获取连续的数字

func main() {
     arrays:= make(chan []int, 5)
     num := make(chan int,2)
    go getContinuityNumberChan(num,3,arrays)
    array:= []int{12,13,14,23,24,34,45,46,47,48,56}
    for i:=0;i<len(array) ;i++  {
        num<-array[i]
    }
    time.Sleep(time.Millisecond)
    close(num)
    elem,_:=<-arrays
    fmt.Println(elem)
}

//num必须是有序的一个一个传入
//count连续几个连续的
//continuity查询后传查询结果给通道
func getContinuityNumberChan(num chan int,count int,continuity chan []int) {
    var e int
    var arrays [][]int
    var arri []int
    ok:=true
    max:=0
    i:=0
    for   {
        select{
        case e,ok=<-num:
            if !ok {
                //End
                if len(arrays)>0 {//当有一组数据时,直接返回数据
                    continuity<-arrays[0]
                    break
                }
                if len(arrays)==0 {//当为空的话就返回一个空的数组
                    var arr []int
                    continuity<-arr
                    break
                }
                break
            }else{
                if i==0 {
                    max=e-1
                }
                i++
                if e!=max+1 {
                    if arri!=nil&&len(arri)>count {//将数组添加到二维数组中
                        arrays=append(arrays, arri)
                    }
                    arri=make([]int,0)
                }
                arri=append(arri,e )//
                max=e//最大值编程元素本身
            }
            if len(arrays)>0 {//当获得一组数据时就停止,直接返回数据
                continuity<-arrays[0]
                ok=false
                break
            }
        }
        if !ok {
            break
        }
    }
}

利用channel将数组从小到大排序

func main() {
     arrays:= make(chan []int, 5)
     num := make(chan int,2)
    go sortMinToMax(num,arrays)
    array:= []int{12,45,46,47,48,56,12,13,14,23,24,34}

    for i:=0;i<len(array) ;i++  {
        num<-array[i]
    }
    time.Sleep(time.Millisecond)
    close(num)
    elem,_:=<-arrays
    fmt.Println(elem)
}

//array返回的array
//num传入的数字
func sortMinToMax(num chan  int,arrays chan []int)  {
     array:=make([]int,0)
    go func() {
        var e int
        ok:=true
        for   {
            select{
            case e,ok=<-num:
                if !ok {
                    //End
                    break
                }else{
                    //continue
                    if len(array)==0 {
                        array=append(array, e)
                    }else {
                        if len(array)==1 {
                            if array[0]>e {
                                a:=array[0]
                                array[0]=e
                                array=append(array,a )
                            }else {
                                array=append(array,e)
                            }
                        } else{
                            for i:=0;i<len(array) ;i++  {
                                if array[i]>e&&i<len(array)-1{
                                    arrstar:=append([]int{},array[:i]...)
                                    arrend:=append([]int{},array[i:]...)
                                    arrstar=append(arrstar, e)
                                    array=append(arrstar, arrend...)
                                    break
                                }
                                if i==len(array)-1  {
                                    array=append(array,e)
                                    break
                                }
                            }
                        }
                    }
                }
            }
            if !ok {
                break
            }
        }
        arrays<-array
    }()
}

利用标准库堆(heap)堆排序实现

package main

import (
    "fmt"
    "container/heap"
    "sort"
)
// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
    // Push and Pop use pointer receivers because they modify the slice's length,
    // not just its contents.
    *h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
    old := *h
    n := len(old)
    x := old[n-1]
    *h = old[0 : n-1]
    return x
}

// This example inserts several ints into an IntHeap, checks the minimum,
// and removes them in order of priority.
func main() {
    h := &IntHeap{100,16,4,8,70,2,36,22,5,12}
    fmt.Println("\nHeap:")
    heap.Init(h)

    fmt.Printf("最小值: %d\n", (*h)[0])

    //for(Pop)依次输出最小值,则相当于执行了HeapSort
    fmt.Println("\nHeap sort:")
    heap.Push(h, 3)
    for h.Len() > 0 {
        fmt.Printf("%d ", heap.Pop(h))
    }

    //增加一个新值,然后输出看看
    fmt.Println("\nPush(h, 3),然后输出堆看看:")
    heap.Push(h, 3)
    for h.Len() > 0 {
        fmt.Printf("%d ", heap.Pop(h))
    }
}

利用sort库排序实现

package main

import (
    "fmt"
    "container/heap"
    "sort"
)
// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
    // Push and Pop use pointer receivers because they modify the slice's length,
    // not just its contents.
    *h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
    old := *h
    n := len(old)
    x := old[n-1]
    *h = old[0 : n-1]
    return x
}
func main() {
    fmt.Println("\n使用sort.Sort排序:")
    h2 := IntHeap{100,16,4,8,70,2,36,22,5,12}
    sort.Sort(h2)
    for _,v := range h2 {
        fmt.Printf("%d ",v)
    }
}
上一篇下一篇

猜你喜欢

热点阅读