This is a creation in Article, where the information may have evolved or changed.
In learning Golang language, the document is really boring, think of the common algorithm to do a realization, writing to learn, want to effect or good!
1. Heap Sequencing
Package Mainimport "FMT" func buildheap (array []int, length int) {var i, J Int; For i = 1; i < length; i = i + 1 {for j = i; j > 0 && array[j] > array[(j-1)/2]; j = (j-1)/2 {array[ J], array[(j-1)/2] = array[(j-1)/2], Array[j]}}}func heapsort (array []int, length int) { Array[0], array[length-1] = array[length-1], array[0] If length <= 2 {return} I, J:= 0, 0 for {j = 2 * i + 1 if j + 1 < length-1 {if ARRAY[J] < Arra Y[j + 1] {j = j + 1}} else if J >= length-1 {break } Array[i], array[j] = Array[j], array[i] i = j} heapsort (array, Length- 1)}func Main () {primes: = [6]int{3, one, 5, 2, 7} fmt. Println ("orginal", primes) Buildheap (primes[:], Len (primes)) Fmt. Println ("Max heap", primes) Heapsort (primes[:], Len (primes)) fmt. Println ("After sorting", primes)}/** out:orginal [3 5 2 7] Max Heap [13 11 7 2 3 5 ] After sorting [2 3 5 7 11 13] **/
2. Bubble sort
package mainimport "fmt"func BubbleSort(vector []int) { fmt.Println("BubbleSort") fmt.Println(vector) for i := 0; i < len(vector); i++ { tag := true // 为了剪枝 // 每一趟将最大的数冒泡 for j := 0; j < len(vector)-i-1; j++ { if vector[j] > vector[j+1] { /*vector[j] < vector[j+1]*/ temp := vector[j] vector[j] = vector[j+1] vector[j+1] = temp tag = false } } if tag { break //0~len(vector)-i没有发生交换说明已经有序 } fmt.Println(vector) }}func main() { primes := [6]int{3, 11, 5, 2, 13, 7} fmt.Println("orginal", primes) BubbleSort(primes[:]) fmt.Println("after sorting", primes)} /** out: orginal [3 11 5 2 13 7] BubbleSort [3 11 5 2 13 7] [3 5 2 11 7 13] [3 2 5 7 11 13] [2 3 5 7 11 13] after sorting [2 3 5 7 11 13] **/
3. Insert Sort
Package Mainimport "FMT" func insertsort (vector []int) {fmt. Println ("Insertsort") fmt. Println (vector) for i: = 1; i < Len (vector); i++ {//each trip does not meet the conditions to choose I for Sentinel Save, the Sentinel inserted into the 0~i-1 ordered sequence (0~i-1 is always ordered) if Vector[i] < Vector[i-1] {/*vector[i] > Vect or[i-1]*/Temp: = Vector[i]//move back until you find the right position for the Sentry J: = I-1 for; J >= 0 && vector[j] > temp; j--{/*vector[j] < temp*/vector[j+1] = vector[j]}//insertion position is ordered before and after, and finally orderly VECTOR[J+1] = temp} fmt. Println (vector)}}func main () {primes: = [6]int{3, one, 5, 2, 7} fmt. Println ("orginal", primes) Insertsort (primes[:]) fmt. Println ("After sorting", primes)}/** out:orginal [3 5 2 7] insertsort [3 11 5 2 13 7] [3 11 5 2 13 7] [3 5 11 2 13 7] [2 3 5 11 13 7] [2 3 5 11 13 7] [2 3 5 7] After sorting [2 3 5 7 11 13]**/
4. Select sort
package mainimport "fmt"func SelectSort(vector []int) { fmt.Println("SelectSort") fmt.Println(vector) for i := 0; i < len(vector); i++ { // 选择最小的元素 k := i for j := i + 1; j < len(vector); j++ { if vector[k] > vector[j] { k = j } } // 交换 if k != i { temp := vector[i] vector[i] = vector[k] vector[k] = temp } fmt.Println(vector) }}func main() { primes := [6]int{3, 11, 5, 2, 13, 7} fmt.Println("orginal", primes) SelectSort(primes[:]) fmt.Println("after sorting", primes)}/** out: orginal [3 11 5 2 13 7] SelectSort [3 11 5 2 13 7] [2 11 5 3 13 7] [2 3 5 11 13 7] [2 3 5 11 13 7] [2 3 5 7 13 11] [2 3 5 7 11 13] [2 3 5 7 11 13] after sorting [2 3 5 7 11 13]**/
5. Two USD Select sort
Package Mainimport "FMT" func binaryselectsort (vector []int) {fmt. Println ("Selectsort") fmt. Println (vector) N: = Len (vector) for i: = 0; i < N/2; i++ {//Select the smallest element and maximum element k: = i t: = n-i-1 for j: = i + 1; j <= N-i-1; VECTOR[K] > vector[j] {k = j} if Vector[t] < Vector[j] {t = J}}//exchange if k! = I {temp: = Vector[i] vector[i] = vector[k] VECTOR[K] = temp} if T! = n-i-1 {temp: = vector[n-i-1] vector[n-i-1] = vector [t] vector[t] = temp} fmt. Println (vector)}}func main () {primes: = [6]int{3, one, 5, 2, 7} fmt. Println ("orginal", primes) Binaryselectsort (primes[:]) fmt. Println ("After sorting", primes)}/** out:orginal [3 5 2 7] selectsort [3 11 5 2 13 7] [2 11 5 3 7 13] [2 35 7] [2 3 5 7] After sorting [2 3 5 11 7 13]**/
6. Quick Sort