Quick sort, NULL interface//1 reference point Pivot:arr[low] arr[mid] Arr[high] Median//2 place pivot position//3 binary sort//4 The number of elements to be sorted is less than 4 general sort algorithm//5 the go language can be exchanged directly, No need for Swappackage Mysort1import ("FMT" "Log") func median3 (arr []interface{}, low, high int) (pivot interface{}) {/ /var pivot int Mid: = (low + high)/2//arr[low] place three minimum values if!compare (Arr[low], Arr[mid]) {Arr[low], arr[m ID] = Arr[mid], Arr[low]} if!compare (Arr[low], Arr[high]) {Arr[low], arr[high] = Arr[high], Arr[low]} Arr[high] Place three Max if!compare (Arr[mid], Arr[high]) {Arr[mid], arr[high] = Arr[high], Arr[mid]} pivot = Arr[mid]//Exchange arr[high-1] for Arr[mid] value arr[mid], arr[high-1] = arr[high-1], Arr[mid]//Return reference pivot return pivot} QuickSort func QuickSort (arr []interface{}, low, high int) in MYSORT1 package {if low > High | | High > (LEN (arr)-1) { Log. PANICLN ("Input error:low > High")} if (Low + 3) <= High {pivot: = MEDIAN3 (arr, low, high)//f Mt. Println ("Pivot:", Pivot)//fmt. Println ("Pivot arr:", arr) I, J: = Low+1, (high-2) for {///left for { If compare (Arr[i], pivot) {i++} else {break} }//fmt. Println ("arr[i]:", arr[i])//right side for {if!compare (Arr[j], pivot) { j--} else {break}}//fmt. Println ("arr[j]:", arr[j])//switch large number to the right of pivot//swap decimal to the left of pivot if I < J { Arr[i], arr[j] = Arr[j], Arr[i]} else {break//pivot find a suitable location}}//pivo T place the appropriate position arr[i], arr[high-1] = arr[high-1], arr[i]//fmt. Println ("arr:", arr)//Sub-QuickSort (arr, Low, i-1) QuickSort (arr, i+1, High)} else {//To be sorted The number of elements is less than 4, and the Simplesort function handles Simplesort (arr, low, high)}}func simplesort (arr []interface{}, low, High int) {if (high-low + 1) < 1 {fmt. Println ("arr is empty")} if (high-low + 1) = = 1 {} if (high-low + 1) = = 2 {if!compare (Arr[low], Arr[high]) {Arr[low], arr[high] = Arr[high], Arr[low]}} if (High-Low + 1) = = 3 {Medi AN3 (arr, low, high)}}//compare NULL interface parameter size//for mergesort and other sorting algorithms package Mysort1import ("log")//compare functions can be placed into other pakcage such that other sorting algorithms can call//This study compare placed in the $GOPATH/SRC/MYSORT1 package//If compare and quicksort in the same package can call the Func compare (left, right interface{}) bool { Switch left. (type) {//the array to be sorted into the corresponding type and compare//NULL interface No comparison case Int:if left. (int) < right. (int) {return true} else {return false} case Float32:if left. (float32) < right. (float32) {return true} else {return false} case Float64:if left. (float64) < right. (float64) {return True } else {return false} Default:log. PANICLN ("Ilegal type:int float32 float64")} return False}//quicksort function Call Example//mergesort Usage Example package Mainimport (" FMT "" "MySort1") func main () {//test quicksort:int float32 float64 quicksorttest ()}func quicksorttest () {//ar R1: = []int{4, 2, 1, 3, 5, 0, 8, +,,} arr1: = []float32{4.0, 2.1, 1.1, 3.2, 5, 0, 8, 20, 100.5, 44, 50} var arr = make ([]interface{}, Len (arr1)) for I, _: = Range arr {Arr[i] = arr1[i]} fmt. Println ("Before Meidan3:", arr) mysort1.quicksort (arr, 0, Len (arr)-1) fmt. Println ("After Meidan3:", arr)}
This paper is a fast sorting algorithm based on Go language, merge sort algorithm and go language defer usage See: https://github.com/Beginner18/myBaseCode.git
Defer:src/defer
Sorting algorithm: Src/mysort1
291 reads ∙1 likes