標籤:bing int func UNC lse AC 基本文法 color style
剛開始接觸go語言,有些不適應,整體和C有些相像,所以學起來也不太困難,但是剛接觸也感覺到了go在很多方面的強大。掌握了go的基本文法,簡單寫個歸併排序練習一下:
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func hebing(arr []int8, left int8, mid int8, right int8) { 8 var arr2 [10]int8 9 i, j := left, mid + 110 cnt := 011 for i <= mid && j <= right {12 if arr[i] < arr[j] {13 arr2[cnt] = arr[i]14 cnt += 115 i += 116 } else {17 arr2[cnt] = arr[j]18 cnt += 119 j += 120 }21 }22 for i <= mid {23 arr2[cnt] = arr[i]24 cnt += 125 i += 126 }27 for j <= right {28 arr2[cnt] = arr[j]29 cnt += 130 j += 131 }32 for tm1, tm2 := 0, left; tm2 <= right; tm1, tm2 = tm1 + 1, tm2 + 1 {33 arr[tm2] = arr2[tm1]34 }35 }36 37 func mergesort(arr []int8, left int8, right int8) {38 if left >= right {39 return40 }41 mid := (left + right) / 242 fmt.Println(left, mid, right)43 mergesort(arr, left, mid)44 mergesort(arr, mid + 1, right)45 hebing(arr, left, mid, right)46 }47 48 func main() {49 var arr = []int8{1, 4, 2, 5, 3}50 mergesort(arr, 0, 4)51 fmt.Println(arr) 52 }
Go初接觸之歸併排序