# The principle of quick sort algorithm and its implementation in go language

Time：2020-7-9

Quick sort is an important sorting algorithm based on divide and conquer technology. Unlike merge sort, which divides elements according to their positions in the array, quick sort divides elements by their values. Specifically, it rearranges the elements in a given array to get a quick sort partition. In a partition, all elements before s subscript are less than or equal to a [S], and all elements after s subscript are greater than or equal to a [S]. Obviously, after a partition is established, a [S] is already in its final position in the ordered array. Next, we can continue to sort the subarrays before and after a [S] (using the same method).
In order to sort all the elements of an array a, quicksort (a, 1, A. length) is called initially.

The following algorithm partitions a [p.. R].

``````
PARTITION(A,p,r)

x = A[r]

i = p - 1

for j = p to r - 1

if A[j] ≤ x

i = i + 1

exchange A[i] with A[j]

exchange A[i+1] with A[r]

return i+1
``````

The efficiency of quick sort algorithm is as follows

In the optimal case, the number of key value comparison CBEST (n) satisfies the following recursive formula:

When n > 1, CBEST (n) = 2cbest (n / 2) + N, CBEST (1) = 0

According to the main theorem, CBEST (n) ∈ Θ (nlogn); for n = 2K, CBEST (n) = NLog (n) is obtained.

In the worst case, all split points tend to be extreme: one of the two subarrays is empty and the other is only one element less than the partitioned array. Specifically, this unfortunate situation can occur on an ascending array, which means that the input array is already sorted. Therefore, after n + 1 comparison, the partition is established, and after a  is exchanged with itself, the quick sort algorithm also sorts the strictly increasing array a [1.. n-1]. Sorting a reduced strictly increasing array continues until the last subarray a [n-2.. n-1]. In this case, the total number of key value comparisons should be equal to:

Cworst(n)=(n+1)+n+…+3=(n+1)(n+2)/2-3∈Θ(n2)

Now it’s time to talk about the efficiency of quicksort on average. For random arrays of size n, the average number of key value comparisons for quick sort is recorded as cavg (n). Assuming that the probability of the split point s (0 ≤ s ≤ n-1) of the partition is 1 / N, we obtain the following recurrence relation: Cavg(0)=0，Cavg(1)=0

Cavg(n)≈2nlnn≈1.38nlogn
Therefore, on average, quicksort performs only 38% more comparison operations than the optimal case. In addition, its innermost loop efficiency is very high, which makes the processing speed of random array faster than merge sort.

Here is the go code for quick sort:

Copy codeThe code is as follows:

func QuickSort(slice_arg []int, iLeft int, iRight int) {
if iLeft < iRight {
var iTmpVal = slice_arg[iLeft]
var i, j = iLeft, iRight
for i < j {
fmt.Println(“i,j = “, i, j)
for i < j && slice_arg[j] > iTmpVal {
j–
}
if i < j {
slice_arg[i] = slice_arg[j]
i++
}

for i < j && slice_arg[i] < iTmpVal {
i++
}
if i < j {
slice_arg[j] = slice_arg[i]
j–
}
}
slice_arg[i] = iTmpVal

QuickSort(slice_arg, iLeft, i-1)
QuickSort(slice_arg, j+1, iRight)
}
}

## Use of String

1. Use of String String s1 = “abc”;//How literals are defined String s2 = “abc”; System.out.println(s1==s2)//true, s1, s2 point to the same address   1. String is declared final and cannot be inherited2. The String class implements the Serializable interface: indicating that strings support serialization,Implements the Comparable interface: indicates that String can be compared in […]