Created
October 4, 2014 19:29
-
-
Save PirosB3/81e0cec32d2f171e8232 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"runtime" | |
) | |
func qsort(data []int, mychan chan(bool)) { | |
if len(data) < 2 { | |
mychan <- true | |
return | |
} | |
pivot := data[0] | |
i, j := 1, len(data)-1 | |
for i <= j { | |
for i <= j && data[i] <= pivot { | |
i++ | |
} | |
for j >= i && data[j] >= pivot { | |
j-- | |
} | |
if i > j { | |
data[0], data[j] = data[j], data[0] | |
newchan := make(chan bool) | |
go qsort(data[0:j], newchan) | |
go qsort(data[i:], newchan) | |
<- newchan | |
<- newchan | |
mychan <- true | |
} else { | |
data[i], data[j] = data[j], data[i] | |
} | |
} | |
} | |
func main() { | |
c := make(chan bool) | |
runtime.GOMAXPROCS(2) | |
n := 500000 | |
nums := make([]int, n) | |
for i:=0; i < n; i++ { | |
nums[i] = rand.Intn(50) | |
} | |
go qsort(nums, c) | |
<- c | |
fmt.Println(nums) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment