SoFunction
Updated on 2025-03-05

Go language shows the entire process of quick sorting algorithm and code examples

Quick sorting algorithm
Quick sorting is a recursive idea. First, select a number as the cardinality, place the number smaller than it in the array to its left, put the number larger than it to its right, and then sort the numbers on the left and right sides recursively.

The key part of the algorithm is to implement the division of arrays, that is, how to divide the elements of the array into two parts, so that the number on the left is smaller than the base and the number on the right is larger than the base. There are many different implementation methods for division. Here we mainly use one-way scanning method. We will briefly introduce the two-way scanning method later.

Select the rightmost number as the base. Use a variable j to record the rightmost subscript value of the current left number (number smaller than the base). Then use the variable i to traverse the array from left to right. If a[i] is smaller than the cardinality, it means that a[i] belongs to the left number, then increase j and then exchange a[j] and the current a[i]. Because j before the increase is the rightmost subscript of the left number, a[j] after the increase is definitely not the left side. After exchanging it with a[i], the new a[j] belongs to the left, and at this time j also becomes the rightmost subscript of the left number again.

After the scan is completed, increase j (because a[j] will be exchanged to the rightmost, so you need to select the number belonging to the right) and exchange it with the rightmost cardinality. At this time, j is the result of the division.

Golang version implementation example:

Copy the codeThe code is as follows:

package main
import "fmt"
 
type ElemType int;
 
func main() {
    data := make([]ElemType, 600000) // ALL ZERO
    var i int = 0;
    var dlen int = len(data);
    for i = 0 ; i < dlen ; i++{
        data[i] = (ElemType)(dlen - i -1);
    }
    ("Start ...",len(data));
    for i = 0 ; i < 100 ; i++{
        ("%d ", data[i]);
    }
    ();
    QuickSort(data,0,dlen-1);
    
    ("End ...");
    for i = 0 ; i < 100 ; i++{
        ("%d ", data[i]);
    }
    ();
}
 
func QuickSort(A []ElemType,low, high int){
    if low < high {
        // Partition() is the operation of divide A[low ... high]
        // one to two arrays which can be used as QuickSort Again
        pivotpos := Partition(A,low,high);
        QuickSort(A,low,pivotpos-1);
        QuickSort(A,pivotpos+1,high);
    }
}
 
func Partition(A []ElemType,low ,high int)  int {
    var pivot ElemType = A[low];
    var tmp ElemType;
    //Method I:
    //for low < high {
    //  for low < high && A[high] >= pivot { high-- ; }
    //  A[low] = A[high];
    //  for low < high && A[low] < pivot { low++; }
    //  A[high] = A[low];
    //}
    //end of MI
    
    //Method II:
    for (low < high) && (A[high] > pivot) { high --; }
    for (low < high) && (A[low] < pivot) {low++; }
    for low < high {
        // swap A[low] & A[high]
        tmp = A[low];
        A[low] = A[high];
        A[high] = tmp;
        low ++;
        high --;
    }
    //end of MII
 
    A[low] = pivot ;
    return low ;
}


The execution output is as follows:
[yu@argcandargv-com quicksort]$ go build  
[yu@argcandargv-com quicksort]$ ls

quicksort 
[yu@argcandargv-com quicksort]$ time ./quicksort
Start ... 600000
599999 599998 599997 599996 599995 599994 599993 599992 599991 599990 599989 599988 599987 599986 599985 599984 599983 599982 599981 599980 599979 599978 599977 599976 599975 599974 599973 599972 599971 599970 599969 599968 599967 599966 599965 599964 599963 599962 599961 599960 599959 599958 599957 599956 599955 599954 599953 599952 599951 599950 599949 599948 599947 599946 599945 599944 599943 599942 599941 599940 599939 599938 599937 599936 599935 599934 599933 599932 599931 599930 599929 599928 599927 599926 599925 599924 599923 599922 599921 599920 599919 599918 599917 599916 599915 599914 599913 599912 599911 599910 599909 599908 599907 599906 599905 599904 599903 599902 599901 599900 
End ...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 

 

real  1m55.564s
user  1m55.215s
sys 0m0.052s

PS:In fact, there is an optimization in the application, because the complexity of quick sorting will degrade to O(n^2) when the array is originally ordered. To avoid this, you can randomly choose when selecting the cardinality. The specific method is to exchange the rightmost number with a random number. There is also a method of taking three numbers in the middle, that is, select the median of three numbers in the beginning and the end and the middle number as the base.