1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
PackageMainImport("FMT""Math/rand""Sort""Time""Github.com/psilva261/timsort")funcPartition (A []int, Lo, HIint)int{pivot: = a[hi]i: = Lo-1 forJ: = Lo; J < Hi; J + + {ifA[J] < pivot {I++A[J], a[i] = A[i], a[j]}}a[i+1], A[hi] = A[hi], a[i+1]returni +1}funcQuickSort (A []int, Lo, HIint) {ifLo >= Hi {return}p: = Partition (A, lo, HI) quickSort (A, lo, p-1) QuickSort (A, p+1, HI)}funcQuicksort_go (A []int, Lo, HIint, doneChan struct{}, Depthint) {ifLo >= Hi {done <-struct{}{}return}depth--p: = Partition (A, lo, HI)ifDepth >0{Childdone: = Make(Chan struct{},2)GoQuicksort_go (A, lo, p-1, Childdone, depth)GoQuicksort_go (A, p+1, Hi, childdone, depth) <-childdone<-childdone}Else{QuickSort (A, lo, p-1) QuickSort (A, p+1, HI)}done <-struct{}{}}funcMain () {rand. Seed (time. Now (). Unixnano ()) testData1, TestData2, testData3, testData4: = Make([]int,0,100000000), Make([]int,0,100000000), Make([]int,0,100000000), Make([]int,0,100000000) Times: =100000000 forI: =0; I < times; i++ {val: = rand. Intn(20000000) TestData1 =Append(TestData1, val) testData2 =Append(TestData2, val) testData3 =Append(TestData3, val) testData4 =Append(TestData4, Val)} Start: = time. Now () QuickSort (TestData1,0,Len(testData1)-1) FMT. Println ("single goroutine:", time. Now (). Sub (start))if!sort. Intsaresorted (testData1) {fmt. Println ("wrong Quick_sort implementation")}done: = Make(Chan struct{}) Start = time. Now ()GoQuicksort_go (TESTDATA2,0,Len(TestData2)-1, Done,5) <-donefmt. Println ("multiple goroutine:", time. Now (). Sub (start))if!sort. Intsaresorted (TESTDATA2) {fmt. Println ("wrong Quicksort_go implementation")}start = time. Now () sort. Ints (TESTDATA3) fmt. Println ("std lib:", time. Now (). Sub (start))if!sort. Intsaresorted (testData3) {fmt. Println ("wrong Std lib implementation")}start = time. Now () Timsort. Ints (TESTDATA4,func(A, Bint)BOOL{returnA <= B}) fmt. Println ("Timsort:", time. Now (). Sub (start))if!sort. Intsaresorted (TESTDATA4) {fmt. Println ("wrong Timsort implementation")}} |