SoFunction
Updated on 2025-03-01

Golang multi-threaded sorting enables fast and efficient processing of large-scale data

Preface

This case implements a multithreaded sorting algorithm that can sort a given array of integers and optimize it concurrently using goroutines.

Random number generator

func randProduce(randNums chan []int, wg *) {
	for i := 0; i < 100; i++ {
		go rand1(randNums, wg)
	}
}
func rand1(randNums chan []int, wg *) {
	r := ((().Unix()))
	int1000 := make([]int, 1000000)
	for i := 0; i < 1000000; i++ {
		int1000[i] = (1000000)
	}
	randNums <- int1000
	()
}

Sorting individual subarrays concurrently using goroutines

func sort0(randNums chan []int, sortNums chan []int, wg *) {
	for i := 0; i < 100; i++ {
		go sort2(randNums, sortNums, wg)
	}
}
func sort2(randNums chan []int, sortNums chan []int, wg *) {
	int1000_Old := <-randNums
	(int1000_Old)
	sortNums <- int1000_Old
	()
}

Merge sorted subarrays to get the final sorted result

func mergeAll(sortNums chan []int, wg *) []int {
	defer ()
	temp2 := &lt;-sortNums
	var temp1 []int
	for i := 1; i &lt;= 99; i++ {
		temp1 = make([]int, 1000000*i+1000000)
		copy(temp1, temp2)
		temp1 = merge(temp1, 1000000*i+1000000, &lt;-sortNums, 1000000)
		temp2 = make([]int, 1000000*i+1000000)
		copy(temp2, temp1)
	}
	return temp2
}
func merge(nums1 []int, m int, nums2 []int, n int) []int {
	temp := make([]int, m)
	copy(temp, nums1)
	t, j := 0, 0 //t is the index of temp, j is the index of nums2	for i := 0; i &lt; len(nums1); i++ {
		if t &gt;= len(temp) {
			nums1[i] = nums2[j]
			j++
			continue
		}
		if j &gt;= n {
			nums1[i] = temp[t]
			t++
			continue
		}
		if nums2[j] &lt;= temp[t] {
			nums1[i] = nums2[j]
			j++
		} else {
			nums1[i] = temp[t]
			t++
		}
	}
	return nums1
}

main function control process

func main() {
	("Start run!")
	start := () // Get the current time	wg := {}
	(201)
	randNums := make(chan []int, 100)
	sortNUms := make(chan []int, 100)
	go randProduce(randNums, &amp;wg)
	go sort0(randNums, sortNUms, &amp;wg)
	go mergeAll(sortNUms, &amp;wg)
	()
	// (l)
	elapsed := (start)
	("This function takes time to complete execution:", elapsed)
}

Ideas

This case uses two channels to store the generated random number slices and the sorted slices. Each slice has a size of 1 million, with a total of 100 slices, which is 100 million data.

randNums := make(chan []int, 100)
	sortNUms := make(chan []int, 100)

While generating random numbers, the program sends the generated random numbers randNums to the sort function for sorting. After sorting the order, the data is sent to sortNUms. These two processes can be computed in parallel, so:

go randProduce(randNums, &wg)
	go sort0(randNums, sortNUms, &wg)

Merge can also participate in parallel calculations, just add one more semaphore:

go mergeAll(sortNUms, &wg)

Running results:

(base) luliang@shenjian Sort % go build

(base) luliang@shenjian Sort % ./SortRoutine

Start running!

This function takes time to complete execution: 50.317081625s

Performance comparison

You can write a single-threaded sort, but the data generation is still multi-threaded:

package main
import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)
func main() {
	("Start run!")
	start := () // Get the current time	randNums := make(chan int, 10000)
	go randProduce1(randNums)
	randNums1 := make([]int, 100000000)
	for i := 0; i &lt; 100000000; i++ {
		randNums1[i] = &lt;-randNums
	}
	(randNums1)
	elapsed := (start)
	("This function takes time to complete execution:", elapsed)
}
func randProduce1(randNums chan int) {
	for i := 0; i &lt; 10000; i++ {
		go rand2(randNums)
	}
}
func rand2(randNums chan int) {
	r := ((().Unix()))
	for i := 0; i &lt; 10000; i++ {
		randNums &lt;- (10000000)
	}
}

The running result is:

(base) luliang@shenjian Sort % go build

(base) luliang@shenjian Sort % ./SortRoutine1

Start running!

This function takes time to complete execution: 54.869565792s

It can be seen that the two methods consume about the same time, because the amount of data is still too small. Multi-threaded data generation, sorting, and merging have opened up a large number of coroutines, which will take a certain amount of time.

This is the article about Golang multi-threaded sorting to quickly and efficiently process large-scale data. For more related Golang multi-threaded sorting content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!