Skip to content

Instantly share code, notes, and snippets.

@tmli3b3rm4n
Created April 5, 2019 11:22
Show Gist options
  • Save tmli3b3rm4n/be72b59f811a3da676858508dd839876 to your computer and use it in GitHub Desktop.
Save tmli3b3rm4n/be72b59f811a3da676858508dd839876 to your computer and use it in GitHub Desktop.
GO concurrency with merge sort
func mergeSortAsync(l []int, c chan []int) {
if len(l) < 2 {
c <- l
return
}
if len(l) < 500 { //THIS NUMBER IS CONFIGURABLE.
c <- merge_sort(l)
return
}
mid := len(l) / 2
c1 := make(chan []int, 1)
c2 := make(chan []int, 1)
go merge_sort_async(l[:mid], c1)
go merge_sort_async(l[mid:], c2)
go func() { c <- merge(<-c1, <-c2) }()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment