SoFunction
Updated on 2025-03-01

Detailed explanation of how to implement merge function in merge sorting in Go language channel

Preface

Less than half a year after I first learned about Go language, I got to know Golang by chance. Seeing his concise grammar style and powerful language characteristics, I instantly became interested in learning him.

I have been learning Go recently, but I have no project to practice, so I forced myself to: If I think of something interesting, I can see if I can use Go to implement it, so I have this article.

Implementation process

I believe everyone is familiar with the merge function in the merge sort. There are a lot of articles for searching online, so I won’t go into details here. At the beginning, I used the routine routine, but I found it boring. It was nothing more than "changing soup but not changing the medicine, and I felt like I was writing something in the language I was familiar with."

Thinking of Go's channel that seems to meet my requirements to some extent, plus things like Goroutine, I wonder: Can you also use these two language features?

channel The data structure has rich meanings in Go, but I basically use it as a queue. The same is true for Goroutine. I basically equate it with "user-state threads" (both are awesome, but as an application developer, sometimes I don't want to go into too much, everything is understood in a simple direction).

Since I'm just practicing, the API I think of looks like this:

Merge(ch1, ch2): outChan

Given two ordered channels, merge them into one ordered channel.

So my implementation is as follows:

func Merge(ch1 <-chan int, ch2 <-chan int) <-chan int {
  out := make(chan int)
  go func() {
    //Wait for upstream data (there is blocking here, which is no different from the regular blocking queue)    v1, ok1 := <-ch1
    v2, ok2 := <-ch2
    // Get data    for ok1 || ok2 {
      if !ok2 || (ok1 && v1 <= v2) {
        // Get the minimum value and push it into out        out <- v1
        v1, ok1 = <-ch1
      } else {
        out <- v2
        v2, ok2 = <-ch2
      }
    }
    // Explicitly close    close(out)
  }()
  // After opening goroutine, the main thread continues to execute and will not block  return out
}

How to use Go

The grammar is almost simple. But it wasn't a big deal for me, I liked it instead. I don't like too many languages ​​with grammatical characteristics (grammatical sugar), messy, distracting too much attention. If each grammatical feature is orthogonal to each other, I naturally agree with it, but if multiple features are doing the same thing, it will generally be a relatively heavy burden on the learner (Ruby), which is not conducive to cooperation.

Compiled type. If I don’t talk much, it is almost a necessary condition for me to learn a new language. The early development efficiency may be slower, but it will be caused by fewer bugs, especially for teamwork, which can reduce a lot of pain. (If you have experts in your team, it will be great to use Python, but this condition is not always met. So use a compiled language :))

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.