Just started to touch the go language, some do not adapt, the whole and C somewhat similar, so it is not too difficult to learn, but just contact also feel that go in many aspects of the strong. Master the basic grammar of Go, simply write a merge sort exercise:
1 Package Main2 3 Import (4 "FMT"5 )6 7 func hebing (arr []int8, left int8, mid int8, right int8) {8 varARR2 [Ten]int89I, J: = left, Mid +1TenCNT: =0 One forI <= mid && J <=Right { A ifArr[i] <Arr[j] { -ARR2[CNT] =Arr[i] -CNT + =1 thei + =1 -}Else { -ARR2[CNT] =Arr[j] -CNT + =1 +J + =1 - } + } A forI <=Mid { atARR2[CNT] =Arr[i] -CNT + =1 -i + =1 - } - forJ <=Right { -ARR2[CNT] =Arr[j] inCNT + =1 -J + =1 to } + forTM1, TM2: =0, left; TM2 <= right; TM1, tm2 = TM1 +1, TM2 +1 { -ARR[TM2] =ARR2[TM1] the } * } $ Panax Notoginseng func mergesort (arr []int8, left int8, right int8) { - ifLeft >=Right { the return + } AMid: = (left + right)/2 the FMT. Println (left, Mid, right) + mergesort (arr, left, mid) -MergeSort (arr, Mid +1, right) $ hebing (arr, left, Mid, right) $ } - - Func Main () { the vararr = []int8{1,4,2,5,3} -MergeSort (arr,0,4)Wuyi FMT. Println (arr) the}
Merge sort of Go first contact