Quick Sort with Golang implementation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First, the fast sort of dancing

Before implementing the sorting algorithm, let us first appreciate a quick sort of video, this video shows the principle of fast sorting, if not understand, please read this article and then look back, you should understand it. O (∩_∩) o~

Second, the rapid sequencing implementation

2.1 Quick Sort Basic edition

With the following set of data, set the leftmost number to the axis and record its value as S.

(Note: * Indicates the number to be exchanged, [] represents the axis)

    • [41] 24 76* 11 45 64 21 69 19 36*
    • [41] 24 36 11 45* 64 21 69 19* 76
    • [41] 24 36 11 19 64* 21* 69 45 76
    • [41] 24 36 11 19 21 64 69 45 76
    • 21 24 36 11 19 [41] 64 69 45 76

Loop processing:

    1. Make index I look to the right from the left of the sequence until I find a number greater than s
    2. Make index J look to the left from the right of the sequence until it finds a number less than s
    3. If I >= J, then leave the loop
    4. If I < J, the value at the Exchange index I and J two
    5. Swap the left axis with J
    6. Recursive to the left of the axis
    7. Recursive to the right of the axis

By performing the algorithm, the value on the left side of the axis will be less than s, and the value on the right side of the axis will be greater than s, so that the left and right sides of the axis are handed back, so that the order can be completed. In the above example, the value on the left side of 41 is smaller than it, and the value on the right is larger than it, so that it is handed back to the sort completion.

The specific code is as follows:

Package main import ("FMT") const MAX = ten var sortarray = []int{41, +, A, one, one, ten, ten, A, a, a, +} func main () { Fmt. Println ("before Sort:") Show () QuickSort (Sortarray, 0, MAX-1) fmt.         Println ("after sort:") show ()}//Quicksortfunc QuickSort (Sortarray []int, left, right int) {if left < right {                Key: = Sortarray[left] I: = left J: = Right-{for i+1 < MAX { i++ if key <= Sortarray[i] {break}} for J            -1 >= 0 {if key >= Sortarray[j] {break} j-- } If I >= j {break} swap (I, j)} Sortarray[lef T] = sortarray[j] sortarray[j] = key Show () QuickSort (Sortarray, left, j-1) QuickSort (Sortarr Ay, j+1, right)}}//Swap the position of a and bfunc sWAP (A, b int) {Sortarray[a], sortarray[b] = Sortarray[b], Sortarray[a]}//Foreachfunc show () {for _, Value: = Ran GE Sortarray {FMT. Printf ("%d\t", Value)}}

2.2 Quick Sort Upgrade version

In the quick-sort basic version, each time the leftmost element is set to an axis, and as previously said, the acceleration of the fast sort method is the choice of the axis, in this case, only the axis is set as an intermediate element, which is compared with the benchmark of this element, which can increase the efficiency of the fast sorting method.

In this case, take the middle of the element s as a comparison, the same first to the right to find a larger than S index i, and then find smaller than S index J, as long as the two sides of the index has not been rendezvous, the exchange of elements of I and J, this time without the axis of exchange, because in the process of looking for exchange, the axis position of the For example:

41 24 76 11 45 64 21 69 19 36

First left for 0,right is 9, (left+right)/2 = 4 (for the quotient of integers), so the axis is the position of index 4, the comparison of the element is 45, you go to the right than 45, to find a lower than 45 of the smaller exchange:

    • 41 24 76* 11 [45] 64 21 69 19 *36
    • 41 24 36 11 45* 64 21 69 19* 76
    • 41 24 36 11 19 64* 21* 69 45 76
    • [41 24 36 11 19 21] [64 69 45 76]

Once you have done this, you can do the sorting by recursively returning the left parenthesis and the right parenthesis.

The specific code is as follows:

  Package Main import ("FMT") const MAX = ten var sortarray = []int{41, +, A, one, one, ten, ten, A, a, $} fu NC Main () {FMT. Println ("before Sort:") Show () QuickSort (Sortarray, 0, MAX-1) fmt. Println ("after sort:") Show ()} func quickSort (Sortarray []int, left, right int) {if left < right {key:                = sortarray[(Left+right)/2] I: = left J: = Right for {for sortarray[i] < key {                 i++} for Sortarray[j] > key {j--} if I >= j { Break} swaps (I, J)} QuickSort (Sortarray, left, i-1) quickSort (Sortarray, J+1, right)}} Swap the position of A and bfunc swap (a, b int) {Sortarray[a], sortarray[b] = Sortarray[b], Sortarray[a]}//FOREAC Hfunc Show () {for _, Value: = Range Sortarray {fmt. Printf ("%d\t", Value)}}  

2.3 Quick Sort Final version

First of all, the concept of this fast sorting method, which takes the rightmost value s as the criterion of comparison, divides the whole series into three parts, one is less than S, one is greater than S, and one is the unhandled part, as follows:




In the process of sorting, I and J will continue to compare and exchange to the right, and the final sequence will become the following state:



Then the value of S is placed in the middle, followed by the same steps will be left and right on both sides of the sequence to sort the action, as follows:


The entire calculation process, directly extracts the virtual code in the book to explain:

QUICKSORT(A, p, r)     if p < r         then q <- PARTITION(A, p, r)                  QUICKSORT(A, p, q-1)                  QUICKSORT(A, q+1, r) end QUICKSORT PARTITION(A, p, r)     x <- A[r]     i <- p-1     for j <- p to r-1         do if A[j] <= x                  then  i <- i+1                          exchange A[i]<->A[j]     exchange A[i+1]<->A[r]     return i+1 end PARTITION  

A practical example of the calculation is as follows:


The specific code is as follows:

  Package Main import ("FMT") const MAX = ten var sortarray = []int{41, +, A, one, one, ten, ten, A, a, $} fu NC Main () {FMT. Println ("before Sort:") Show () QuickSort (Sortarray, 0, MAX-1) fmt. Println ("after sort:") Show ()} func quickSort (Sortarray []int, left, right int) {if left < right {pos:    = Partition (Sortarray, left, right) partition (Sortarray, left, pos-1) partition (Sortarray, pos+1, right)  }} func partition (Sortarray []int, left, right int) int {key: = Sortarray[right] I: = Left-1 for J: = left; j < right; J + + {if SORTARRAY[J] <= key {i++ swap (I, j)}} swap (i+1, right) retur n i + 1}//Swap the position of A and bfunc swap (a, b int) {Sortarray[a], sortarray[b] = Sortarray[b], Sortarray[a]} Foreachfunc Show () {for _, Value: = Range Sortarray {fmt. Printf ("%d\t", Value)}}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.