Created
April 5, 2019 11:22
-
-
Save tmli3b3rm4n/be72b59f811a3da676858508dd839876 to your computer and use it in GitHub Desktop.
GO concurrency with merge sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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