Basic Sorting Algorithm Golang

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

Summary

Sorting has an internal sort and an external sort, the internal sort is the data record is sorted in memory, and the external sort is because the sorted data is large, one cannot hold all the sort records at a time, and the external memory needs to be accessed during the sorting process.

Bubble sort

1Func bubblesort (Vector []int) {2Fmt. Println ("Bubblesort")3 FMT. Println (vector)4      forI: =0; i < Len (vector); i++ {5Tag: =true //to Prune6         //each trip will bubble up the biggest number7          forJ: =0; J < Len (vector)-i-1; J + + {8             ifVECTOR[J] > vector[j+1] {/*Vector[j] < vector[j+1]*/9Temp: =Vector[j]TenVECTOR[J] = vector[j+1] Onevector[j+1] =Temp ATag =false -             } -         } the         ifTag { -              Break //0~len (vector)-I did not happen exchange instructions have been ordered -         } - FMT. Println (vector) +     } -}

Insert Sort

1Func insertsort (Vector []int) {2Fmt. Println ("Insertsort")3 FMT. Println (vector)4      forI: =1; i < Len (vector); i++ {5         //each trip does not meet the conditions to choose I for Sentinel preservation, the Sentinel inserted into the 0~i-1 ordered sequence (0~i-1 is always orderly)6         ifVector[i] < vector[i-1] {/*Vector[i] > vector[i-1]*/7Temp: =Vector[i]8             //move back until you find the right position for the sentry.9J: = i-1Ten              for; J >=0&& Vector[j] > temp; j--{/*Vector[j] < temp*/ Onevector[j+1] =Vector[j] A             } -             //The insertion position is orderly before and after, and the final is orderly. -vector[j+1] =Temp the         } - FMT. Println (vector) -     } -}

Select sort

1Func selectsort (Vector []int) {2Fmt. Println ("Selectsort")3 FMT. Println (vector)4      forI: =0; i < Len (vector); i++ {5         //Select the smallest element6K: =I7          forJ: = i +1; J < Len (vector); J + + {8             ifVECTOR[K] >Vector[j] {9K =JTen             } One         } A         //Exchange -         ifK! =I { -Temp: =Vector[i] theVector[i] =Vector[k] -VECTOR[K] =Temp -         } - FMT. Println (vector) +     } -}

Binary selection sort

1Func binaryselectsort (Vector []int) {2Fmt. Println ("Selectsort")3 FMT. Println (vector)4N: =Len (vector)5      forI: =0; I < n/2; i++ {6         //Select the smallest element and the largest element7K: =I8T: = n-i-19          forJ: = i +1; J <= n-i-1; J + + {Ten             ifVECTOR[K] >Vector[j] { OneK =J A             } -             ifVector[t] <Vector[j] { -t =J the             } -         } -         //Exchange -         ifK! =I { +Temp: =Vector[i] -Vector[i] =Vector[k] +VECTOR[K] =Temp A         } at         ifT! = n-i-1 { -Temp: = vector[n-i-1] -vector[n-i-1] =Vector[t] -VECTOR[T] =Temp -         } - FMT. Println (vector) in     } -}

Quick Sort

The quick sort is simply to dig a hole and divide it, and it is recognized that efficiency is the best, and this must

1Func QuickSort (Vector []int, Low, hightint) {2 FMT. Println (vector)3     ifLow <hight {4I: = Low5J: =hight6Temp: = Vector[low]//start digging the pit to fill the number7          forI <J {8              forI < J && Vector[j] >=Temp {9j--Ten             } OneVector[i] =Vector[j] A              forI < J && Vector[i] <=Temp { -i++ -             } theVECTOR[J] =Vector[i] -         } -Vector[i] =Temp -QuickSort (vector, low, I-1)//Divide and conquer +QuickSort (Vector, i+1, hight) -     } +}

Frequently asked questions, known sequence Wausthko, the first number as the base of W, Q:

1, what is the sequence after the first trip? Assuming recursion is simultaneous

1), OAUSTHKW

2), KAHOTsuw

3), HAKOSTUW

4), AHKostuw

2, which of the numbers will be chosen as the cardinality of the sorting process?

The above marks are: W,o,k, t,h

3, the selection of cardinality directly affects efficiency, while the end of the sequence is obviously an efficiency problem that can be replaced by other algorithms.

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.