Skip to content

Instantly share code, notes, and snippets.

@sagivo
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagivo/2b8b81e9cf1b3d48ce51 to your computer and use it in GitHub Desktop.
Save sagivo/2b8b81e9cf1b3d48ce51 to your computer and use it in GitHub Desktop.
mergesort async with limit
func merge_sort_async(l []int, c chan []int) {
if len(l) < 2 {
c <- l
return
}
if len(l) < 500 { //TUNE THIS NUMER AND DONT CREATE EXTRA WORK UNLESS IT'S BIGGER
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