func buildHeap(nums []int, heapSize int) { for i := heapSize / 2; i >= 0; i-- { heapify(nums, i, heapSize) } }
func heapSort(nums []int) { heapSize := len(nums) buildHeap(nums, heapSize) for i := heapSize - 1; i >= 0; i-- { nums[i], nums[0] = nums[0], nums[i] heapify(nums, 0, i) } }
func heapify(nums []int, i, heapSize int) { largest := i left, right := 2*i+1, 2*i+2 if left < heapSize && nums[left] > nums[largest] { largest = left } if right < heapSize && nums[right] > nums[largest] { largest = right } if largest != i { nums[largest], nums[i] = nums[i], nums[largest] heapify(nums, largest, heapSize) } }
func Even(wg *sync.WaitGroup, ch chan int) { defer wg.Done() for i := 0; i <= 100; i += 2 { ch <- i } }
func Odd(wg *sync.WaitGroup, ch chan int) { defer wg.Done() for i := 1; i <= 100; i += 2 { ch <- i } }
func main() { var wg sync.WaitGroup ch := make(chan int) wg.Add(2) go Even(&wg, ch) go Odd(&wg, ch) go func() { wg.Wait() close(ch) }() for v := range ch { fmt.Println("number:", v) } }