SoFunction
Updated on 2025-03-05

Example of using general qsort function of Go sorting algorithm

Overview

QuickSort is a classic sorting algorithm whose efficiency and wide application make it a treasure in the field of computer science.

This article will introduce how to encapsulate quick sort functions in Go to make them easier to use and more versatile, and provide readers with in-depth understanding of their principles and implementations through examples and code explanations.

1. Introduction to Quick Sorting Algorithm

1.1 Algorithm Principles

Quick sorting is a sorting algorithm for dividing and conquer strategies. The basic idea is to select a benchmark element.

Divide the sequence into two parts, put the elements smaller than the reference to the left and elements larger than the reference to the right, and then quickly sort the left and right subsequences recursively.

1.2 Sample Code

package main
import "fmt"
func quickSort(arr []int) {
  if len(arr) <= 1 {
    return
  }
  pivotIndex := partition(arr)
  quickSort(arr[:pivotIndex])
  quickSort(arr[pivotIndex+1:])
}
func partition(arr []int) int {
  pivot := arr[0]
  left, right := 1, len(arr)-1
  for left <= right {
    for left <= right && arr[left] < pivot {
      left++
    }
    for left <= right && arr[right] > pivot {
      right--
    }
    if left <= right {
      arr[left], arr[right] = arr[right], arr[left]
      left++
      right--
    }
  }
  arr[0], arr[right] = arr[right], arr[0]
  return right
}
func main() {
  arr := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
  quickSort(arr)
  ("Sorted array:", arr)
}

In this example code, the quickSort function implements recursive calls for quick sorting, while the partition function is responsible for selecting the benchmark elements in each round of sorting and segmenting the array.

2. Encapsulate quick sorting functions

2.1 Design ideas

To make quick sorting easier to use and general, wrap it into a separate function and provide parameters to support different types of slice sorting.

2.2 Sample code

package main
import (
  "fmt"
  "reflect"
)
func QuickSort(slice interface{}) {
  value := (slice)
  if () !=  {
    panic("Input is not a slice")
  }
  quickSortGeneric(slice, 0, ()-1)
}
func quickSortGeneric(slice interface{}, low, high int) {
  value := (slice)
  if low < high {
    pivotIndex := partitionGeneric(slice, low, high)
    quickSortGeneric(slice, low, pivotIndex-1)
    quickSortGeneric(slice, pivotIndex+1, high)
  }
}
func partitionGeneric(slice interface{}, low, high int) int {
  value := (slice)
  pivot := (low).Interface()
  left, right := low+1, high
  for left <= right {
    for left <= right && (slice).Index(left).Interface() < pivot {
      left++
    }
    for left <= right && (slice).Index(right).Interface() > pivot {
      right--
    }
    if left <= right {
      swap(slice, left, right)
      left++
      right--
    }
  }
  swap(slice, low, right)
  return right
}
func swap(slice interface{}, i, j int) {
  value := (slice)
  tmp := (i).Interface()
  (i).Set((j))
  (j).Set((tmp))
}
func main() {
  arr := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
  QuickSort(arr)
  ("Sorted array:", arr)
  strArr := []string{"banana", "apple", "orange", "grape"}
  QuickSort(strArr)
  ("Sorted strings:", strArr)
}

In this example, the QuickSort function accepts slices of any type and sorts them using reflection.

Different types of slices are provided, showing how to sort integer and string slices by this general function.

3. Summary

Through this article, readers should have a deeper understanding of the quick sort algorithm and learn how to encapsulate a common quick sort function in the Go language.

This encapsulation improves the reusability of the code, making it easy to use the same sorting algorithm on different types of data.

In actual development, more flexible sorting functions can provide programmers with more choices, making the sorting process more convenient and efficient.

The above is the detailed content of the general qsort function usage example of the Go sorting algorithm. For more information about the Go qsort function sorting algorithm, please pay attention to my other related articles!