Fast sorting algorithm of go code

Time:2020-7-8

Quick sort algorithm
First of all, put the number on the left side of the recursive array, and then put it on the left side of the number, and then put it on the left side of the number.

The key part of the algorithm is to realize the partition of array, that is, how to divide the elements of the array into two parts, so that the number on the left is smaller than the radix, and the number on the right is larger than the radix. There are many different ways to realize the partition. Here we mainly use the one-way scanning method, and then we will introduce the two-way scanning method.

Select the number on the far right as the base. Use a variable J to record the right most subscript value of the current left number (the 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 number on the left. Then J is increased automatically, and then a [J] and the current a [i] are exchanged. Because the j before the self increment is the right most subscript of the left number, the a [J] after the self increment definitely does not belong to the left. After exchanging it with a [i], the new a [J] belongs to the left, and at this time j becomes the rightmost subscript of the left number.

At the end of the scan, the auto increment of J (because a [J] will be switched to the rightmost, so the number belonging to the right) is exchanged with the rightmost cardinal number. At this time, j is the result of partition.

Implementation examples of golang version:

Copy 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);
    }
    fmt.Println(“Start …”,len(data));
    for i = 0 ; i < 100 ; i++{
        fmt.Printf(“%d “, data[i]);
    }
    fmt.Println();
    QuickSort(data,0,dlen-1);
    
    fmt.Println(“End …”);
    for i = 0 ; i < 100 ; i++{
        fmt.Printf(“%d “, data[i]);
    }
    fmt.Println();
}
 
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:


[[email protected] quicksort]$ go build quicksort.go 
[[email protected] quicksort]$ ls

quicksort quicksort.go

[[email protected] 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 quicksort will degenerate to o (n ^ 2) when the array is originally ordered. In order to avoid this, we can choose the base randomly. This is done by swapping the rightmost number with a random number. In addition, there is a method of taking the middle of three numbers, that is, selecting the median of three numbers from the beginning to the end and the middle as the cardinal number.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]