Implementation of Go language sorting algorithm

Source: Internet
Author: User

//Algorithm Project Algorithm.gopackage algorithm//Bubble SortFunc Bubblesort (a []int) {n:=Len (a) forI: = n; i >1; i-- {         forJ: =0; J < i-1; J + + {            ifa[j] > a[j+1] {a[j], a[j+1] = a[j+1], a[j] }}}//Select SortFunc Selectsort (a []int) {n:=Len (a) forI: =0; I < n1; i++{k:=I forJ: = i +1; J < n; J + + {            ifa[j] <a[k] {k=j}} a[i], a[k]=a[k], a[i]}}//Insert SortFunc Insertionsort (a []int) {n:=Len (a) forI: =1; I < n; i++{temp:=a[i] J:= i-1         for; J >=0&& a[j] > temp; j--{a[j+1] =a[j]} a[j+1] =temp}}//Hill SortFunc Shellsort (a []int) {n:=Len (a) ford: = n/2; D >=1; D/=2 {         forI: = d; I < n; i++{temp:=a[i] J:= i-D for; J >=0&& a[j] > temp; j-=D {a[j+d] =a[j]} a[j+d] =temp} }}//One division of fast sortingFunc partition (a []int, SintEint)int{temp:=a[s] I:=S J:=e forI <J { forI < J && a[j] >Temp {j--        }        ifI <J {a[i]=a[j] i++        }         forI < J && A[i] <Temp {i++        }        ifI <J {a[j]=a[i] J--}} a[i]=Tempreturni}//Quick SortFunc QuickSort (a []int, SintEint) {    ifS >=e {return} I:=partition (a, s, e) QuickSort (a, s, i-1) QuickSort (a, i+1, e)}//Heap SortFunc heapsort (a []int) {n:=Len (a)//Build a heap     forI: = n/2-1; I >=0; i--{k:=I for 2*k+1<N {j:=2*k +1            ifj+1< n && a[j] < a[j+1] {j++            }            ifa[j] >a[k] {a[k], a[j]=a[j], a[k] k=j}Else {                 break            }        }    }    //Adjustment Heap     forI: = n-1; i >0; i--{a[0], a[i] = a[i], a[0] K:=0         for 2*k+1<I {j:=2*k +1            ifj+1< I && a[j] < a[j+1] {j++            }            ifa[j] >a[k] {a[k], a[j]=a[j], a[k] k=j}Else {                 break            }        }    }}//Merge OnceFunc Mergeone (a []int, b []intNintLenint) {i:=0     forI+len <N {j:= i +2*len-1        ifJ >=N {j= n-1} m:=i k:=I L:= i +Len forI < K+len && L <=J {ifa[i] <=a[l] {b[m]=a[i] M++I++            } Else{b[m]=a[l] M++L++            }        }         forI < K +Len {b[m]=a[i] M++I++        }         forL <=J {b[m]=a[l] M++L++} i= j +1    }    ifI <N { for; I < n; i++{b[i]=a[i] }}}//Merge SortFunc mergesort (a []int) {n:=Len (a) B:= Make ([]int, N) len:=1flag:=0     forLen <N {ifFlag = =0{mergeone (a, b, n, len)}ifFlag = =1{mergeone (b, a, n, len)} flag=1-Flag Len*=2    }    ifFlag = =1 {         forI: =0; I < n; i++{a[i]=b[i] }}}

GitHub Link Address: https://github.com/gaopeng527/go_Algorithm/blob/master/sort.go

Implementation of Go language sorting algorithm

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.