func Count(arr []int, nLength int) {
ifnil == arr || nLength <= 0 {
return
}
var max int = 0for i := 0; i < nLength; i++ {
if arr[i] > max {
max = arr[i]
}
}
var arrTemp []int = make([]int, max+1)
for i := 0; i < nLength; i++ {
arrTemp[arr[i]]++
}
var j int = 0for i := 0; i < max+1; i++ {
if arrTemp[i] > 0 {
arr[j] = i
arrTemp[i]--
j++
}
}
}
2. shell排序
func Shell(arr []int, nLength int) {
ifnil == arr || nLength <= 0 {
return
}
fmt.Println("nLength:", nLength)
// 确定步长for gap := nLength / 2; gap >= 1; gap /= 2 {
fmt.Println("gap:", gap)
// 对每一个步长对应的数组进行插入排序for i := gap; i < nLength; i += gap {
flag := i - gap
temp := arr[i]
for flag >= 0 && arr[flag] > temp {
arr[flag+gap] = arr[flag]
flag -= gap
}
arr[flag+gap] = temp
}
}
}
3. 合并排序
func Shell(arr []int, nLength int) {
ifnil == arr || nLength <= 0 {
return
}
fmt.Println("nLength:", nLength)
// 确定步长for gap := nLength / 2; gap >= 1; gap /= 2 {
fmt.Println("gap:", gap)
// 对每一个步长对应的数组进行插入排序for i := gap; i < nLength; i += gap {
flag := i - gap
temp := arr[i]
for flag >= 0 && arr[flag] > temp {
arr[flag+gap] = arr[flag]
flag -= gap
}
arr[flag+gap] = temp
}
}
}