Go to achieve the goblin sorting algorithm

In the previous we explained the goblin sorting algorithm in detail, now we use the go language to achieve

Package main

Import "fmt"

//Greek sorting
Func gnomeSort(theArray []int) []int {
i := 0
For i<len(theArray){
If i==0 || theArray[i-1] <= theArray[i]{
i++
}else{
theArray[i-1], theArray[i] = theArray[i], theArray[i-1]
I--
}
}
Return theArray
}

Func main() {
Var theArray = []int{10,1,18,30,23,12,7,5,18,17}
fmt.Print("pre-sort")
fmt.Println(theArray)
fmt.Print("after sorting")
fmt.Println(gnomeSort(theArray))
}

We execute and see the results

Before sorting [10 1 18 30 23 12 7 5 18 17]
After sorting [1 5 7 10 12 17 18 18 23 30]

In line with expectations