An experiment was made to sort 10 million random numbers for 24 seconds. But memory usage is a bit high, and here's the code. Dual-core is used.

Source: Internet
Author: User

Maste.go

Package Mainimport ("FMT" "OS" "Runtime" "Test" "Time") var x chan []int = make (chan []int, 1) var Num int = 100var Wnum int = 1 00func Main () {FMT. Println (time. Now ()) var list1, List2 []intvar R chan []int = Do (chan []int, 1) for I: = 0; i < Wnum; i++ {Go Chan (Num, R)}for I: = 0; i < Wnum; i++ {Select {case y: = <-r:if len (list1) = = 0 {list1 = y} else {list2 = Ylist1 = Test. Merge (List1, List2)}}}fmt. Println (time. Now ()) FMT. Println (Len (list1)) File, _: = OS. OpenFile ("C.txt", OS. O_rdwr|os. O_create, 0777) file.write ([]byte (FMT. Sprint (List1)) file.close ()}func Chan (Num int, R Chan []int) {var list1, List2 []intruntime. Gomaxprocs (2) for I: = 0; i < Num; i++ {go func () {x <-test. Rand_list (+)} ()}for I: = 0; i < Num; i++ {Select {case y: = <-x:if len (list1) = = 0 {list1 = y} else {list2 = Ylist1 = Test. Merge (List1, List2)}}}r <-List1}

Test directory:

Package TestFunc Merge (List1, List2 []int) []int {list1_num: = Len (list1) List2_num: = Len (List2) var J int = 0var List3 []i Ntfor I, _: = Range List1 {this:if list1[i] <= list2[j] {list3 = append (List3, list1[i]) if i = = list1_num-1 {for z: = j ; Z < List2_num; z++ {list3 = append (List3, list2[z])}break}} else {if J <= list2_num-1 {list3 = append (List3, list2[j]) if j = = List2_nu m-1 {for J: = i; j < List1_num; J + + {list3 = append (List3, list1[j])}break}j++goto This}}}return List3}

Package Testimport ("Math/rand") func rand_list (num int) []int {var list []intfor I: = 0; i < Num; i++ {list = append (li St, Rand. INTN (100000))}for I: = 0; i < Num; i++ {for J: = i + 1, J < Num, J + + {if list[i] > List[j] {list[i], list[j] = list[j], List[i]}}}return list}


An experiment was made to sort 10 million random numbers for 24 seconds. But memory usage is a bit high, and here's the code. Dual-core is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.