Sorting algorithm using GO implementation

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

Go implementation of the partial sorting algorithm, to be sorted

Algorithm Project Main.gopackage mainimport ("FMT") func main () {arr: = []int{50, 45, 42, 30, 25, 20, 20, 5, 60, 3, 235, 9}//arr: = []int{50, 235, the FMT. Println (arr) fmt. Println ("----------")//Direct Insert sort//arr1: = Insertsort (arr)//arr1: = Selectsort (arr)//a, B: = Selectminandmax (A RR, 0, +)//arr1: = Selectsortplus (arr) quickSort (arr, 0, Len (arr)-1) fmt. Println (arr)}/******** bubble sort begin ********/func Bubblesort (arr []int) []int {length: = Len (arr) for j: = Length-1 ; J > 0;                j--{for I: = 0; i < J; i++ {if arr[i] > arr[i+1] {Exchange (arr, I, i+1) Fmt. Println (arr)}} FMT. Println ("++++++++++")} return arr}/******** bubble sort end ********//******* Direct Insert sort begin ********///Note the first sort should be the first bit, which is indexed as        0 is seen as an ordered sequence of func insertsort (arr []int) []int {///Gets the current array length: Len (arr) for I: = 1; i < length; i++ {   Current value Now: = Arr[i]     If the current Sentinel is less than the value of one of the k in the previous sequence, the sequence moves from K to the whole of a for j: = I-1; J >= 0;                j--{if ' < Arr[j] {arr[j+1] = arr[j] arr[j] = now} else { Arr[j+1] = Now Break}} FMT.  Println (arr)} return arr}/******* Direct insert sort end ********//******* Select sort-Simple select sort begin ********///Choose the smallest key value/* @param arr The array to be sorted @param i gets the minimum value from the first element of the */func selectmin (arr []int, I int) int {length: = Len (arr) Minkey: = i minValue: = arr[ Minkey]//To find the element with the smallest value from subscript I and subsequent elements for k: = Minkey + 1; K < length; k++ {if minValue > Arr[k] {//If the current value is greater than an element, the description is not the minimum, and after the element exchange Minkey = k Minvalu    e = Arr[k]}} return Minkey}func Exchange (arr []int, a int, b int) {temp: = Arr[a] arr[a] = arr[b]        Arr[b] = temp}//begins the selection sort func selectsort (arr []int) []int {length: = Len (arr) for I: = 0; i < length; i++ {   Each loop finds the smallest value in the currently unordered element, swapping it with the current element     Minkey: = Selectmin (arr, i) of exchange (arr, I, Minkey) fmt. Println (i, arr)} return arr}/******* Select Sort-Simple Select sort End ********//******** Select sorting improvements, two meta select sort begin ********/FUNC Select Minandmax (arr []int, I int, j int) (int, int) {//length: = Len (arr) Minkey: = i minValue: = Arr[minkey] Maxke Y: = J MaxValue: = Arr[maxkey]//Find the element with the lowest value for K from subscript I and the element after: = Minkey + 1; K < J; k++ {if minValue > Arr[k] {//If the current value is greater than an element, the description is not the minimum, and after the element exchange Minkey = k Minvalu e = arr[k]} if MaxValue < Arr[k] {Maxkey = k MaxValue = Arr[k]}} r Eturn Minkey, Maxkey}func selectsortplus (arr []int) []int {length: = Len (arr)//For I: = 0; I <= length/2; i + + {//cycles, find the maximum and minimum values, replace the maximum and minimum ends of the head section Minkey, Maxkey: = Selectminandmax (arr, I, length-1-i) Exchange (a RR, I, Minkey) Exchange (arr, length-1-i, Maxkey) fmt.   Println (Minkey, Maxkey)} Return arr}/******** Select sort of improvement, two Yuan Select sort end ********//******** Quick sort begin ********/func QuickSort (arr []int, left int, right int) {FMT. Println (left, right)//Set datum number, select first as base number basekey: = left BaseValue: = Arr[basekey] I: = left J: = Right F Or I < J {Fmt.        Println (i, j)//right-to-left search until a value less than the base number is found for (Arr[j] >= baseValue) && (i < j) {j--        } If I < J {//Put the value of J on the empty space of I arr[i] = arr[j]}//From left to right until a value greater than the base number is found For (I < J) && (Arr[i] < BaseValue) {i++} if I < J {//Put the I at this time before J The resulting vacancy on arr[j] = arr[i]} fmt. Println (i, J)} Arr[i] = BaseValue FMT.  PRINTLN (arr) if left < i-1 {QuickSort (arr, left, i-1)} if i+1 < right {QuickSort (arr, i+1, right)}}/******** Quick Sort End ********/

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.