# Go language shows the whole process of quicksort algorithm and code examples

• 2020-06-01 10:00:20
• OfStack

Quicksort algorithm
Quicksort is the idea of a recursion, where you first select a number as the cardinality, place the number less than it on the left of the array, and the number greater than it on the right of the array, and then recursively sort the Numbers on the left and right.

The key part of the algorithm is to implement the partition of the 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 cardinality and the number on the right is larger than the cardinality. There are a number of different implementations of partitioning. The one-way scanning method is used here, and the two-way scanning method will be introduced later.

Select the rightmost digit as the cardinality. Use one variable, j, to record the rightmost lower value of the current number on the left (the number smaller than the cardinality). If a[i] is smaller than the cardinality, a[i] belongs to the number on the left, then add j to itself, and then swap a[j] with the current a[i]. Since j before the increment is the right-most subscript of the number on the left, a[j] after the increment definitely does not belong to the left. After swapping it with a[i], the new a[j] belongs to the left, and j also becomes the right-most subscript of the number on the left again.

At the end of the scan, j is added (because a[j] will be swapped to the far right, so choose the number that belongs to the right) and then swapped to the far right cardinality, and j is the result of partition.

Example implementation of Golang version:

``````
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:

``````
[yu@argcandargv-com quicksort]\$ go build quicksort.go
[yu@argcandargv-com quicksort]\$ ls
``````

``````
quicksort quicksort.go
``````
``````
[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: there is actually an optimization in the application, because quicksort degrades to O(n^2) when the array is already ordered. To avoid this, you can pick the cardinality at random. The way to do this is to swap the rightmost number with a random number. In addition, there is a method of picking 3 Numbers, that is, choosing the median value of 3 Numbers in total with some middle number as the cardinal number.

Related articles: