The sort study of Golang

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

Years ago, no money, wait for the pay. So in the company is not afraid to go home, bored to see the next Golang sort source

type Interface Interface {    // Len is the number of elements in the collection.    int    // Less reports whether the element    with // index I should sort before the element with index J.    int ) bool    //  Swap swaps the elements with indexes I and J.    int )}

You can call sort only if you implement this interface. Sort () in the Order of the DA

Paste the source code implementation, this is the usual sort, time complexity is O (N*log (n)). That's the quick platoon, the heap sort, whatever.

 //  Sort sorts data.  //  It makes one call to data. Len to determine N, and O (N*log (n)) calls to  //  data. Less and data. Swap. The sort is isn't guaranteed to be stable.  func Sort (data Interface) {  Switch to Heapsort if depth of 2*ceil (LG (N+1)) is reached.  N: = data. Len () maxDepth:  = 0 for  I: = N; i > 0; I >>= 1 ++} maxDepth *= 2 QuickSort (data,  0 /pre>

It uses the quicksort. However, it also has a maxdepth. Makes me very annoyed. This is not an extra variable. Then go to see the realization of quicksort. I got a rub. The first time I saw this, it was a quick platoon.

Func quickSort (Data Interface, A, B, maxDepthint) {     forB-a > 12 {//Use shellsort for slices <= elements        ifMaxDepth = = 0{heapsort (data, A, b)return} maxDepth--mlo, MHI:=Dopivot (data, A, b)//avoiding recursion on the larger Subproblem guarantees        //a stack depth of at most LG (B-A).        ifMlo-a < bMHI {quickSort (data, A, MLO, maxDepth) a= MHI//i.e., QuickSort (data, MHI, b)}Else{quickSort (data, MHI, B, maxDepth) b= Mlo//i.e., QuickSort (data, A, MLO)        }    }    ifB-a > 1 {        //Do Shellsort pass with Gap 6        //It could is written in this simplified form cause b-a <=         forI: = a + 6; I < b; i++ {            ifData. Less (I, i-6) {data. Swap (i, I-6)}} insertionsort (data, A, b) }}

The

interprets the following variable, a: Start position, B: End position, maxDepth: depth, this parameter is calculated in sort () and is computed by right shift. So This is the depth of the complete binary tree that all elements are built to ha. This dog day in the fast platoon to use Heapsort, calculate the theoretical construction of the complete binary tree depth. It maxdepth *= the other hand, let me smoke a cool calm. It gets the depth of the deepest two-fork tree that these elements make up, master AH. Then take the quicksort code to look at it. When the element that needs to be sorted is greater than 12: Come on, get in the line. It is here to determine whether the depth is 0, confused Ah, what is this? This for loop has two exit conditions, 1:b-a<12. 2:maxdepth==0. So, why would there be maxdepth==0 ah. At this point to sort the element is more than 12, well, it turns out that this is the two counts. Essentially wood has any relationship,

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.