Parallel sorting algorithm for Go language writing (fast sorting)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport "FMT"//Threads thread identity creation thread number func quicksort (nums []int, ch Chan int, level int, threads int) {Level=le   vel*2 If Len (nums) = = 1 {ch<-nums[0]; close (CH); return}//ch<-nums[0] indicates that the nums[0] data is written to the CH channel if Len (nums) = = 0 { Close (CH); return} Less: = "make" ([]int, 0)//Greater: = Make ([]int,0) Left: = Nums[0]//Fast sort Axis Nums = nums[1:]//scan data to the right of the axis Put in the greater less than the for _,num_data: = range nums{switch{case num_data <= left:less = append (less,num_d   ATA) Case Num_data > left:greater = Append (Greater,num_data)}} left_ch: = Do (chan int, len (less))    Right_ch: = Make (chan int, Len (greater)) if (level <= threads) {Go quicksort (less, left_ch, level, threads)//Sub-task Go quicksort (Greater,right_ch, level, threads)}else{quicksort (less,left_ch, level, threads) quicksort (Greater,  Right_ch, level, threads)}//merge data for I: = Range left_ch{ch<-i;  } ch<-left for I: = Range right_ch{ch<-i;} close (CH) return}func Main () {x: = []int{3, 1, 4, 1, 5, 9, 2, 6} ch: = make (chan int) go quicksort (x, ch, 0, 0)//0 0 means no limit on the number of threads for V: = Range (ch) {FMT. Println (v)}}

Related Article

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.