package main
import (
"fmt"
)
/*
TODO:直接插入
*/
func straightInsertionSort(slice []int) {
size := len(slice)
//从第二位开始
for i := 1; i < size; i++ {
temp, j := slice[i], i - 1
for j >= 0 && slice[j] > temp {
slice[j + 1] = slice[j]
j--
}
slice[j + 1] = temp;
}
fmt.Println("直接插入", slice)
return
}
/*
TODO:希尔排序
*/
func shellSort(slice []int) {
size := len(slice)
step := size / 2
for step > 0 {
for i := step; i < size; i++ {
temp := slice[i]
j := i - step
for j >= 0 && slice[j] > temp {
slice[j + step] = slice[j]
j = j - step
}
slice[j + step] = temp
}
step = step / 2
}
fmt.Println("希尔排序", slice)
return
}
/*
TODO:简单选择排序
*/
func selectSort(slice []int) {
size := len(slice)
for i := 0; i < size; i++ {
k := i
for j := i + 1; j < size; j++ {
if slice[j] < slice[k] {
k = j
}
}
if (k != i) {
slice[i], slice[k] = slice[k], slice[i]
}
}
fmt.Println("简单选择", slice)
return
}
/*
TODO:堆排序
*/
func heapBuild(root int, slice []int, size int) {
l := 2 * root + 1
m := l
if l < size {
r := l + 1
if r < size {
if slice[r] > slice[l] {
m = r
}
}
if slice[m] > slice[root] {
slice[m], slice[root] = slice[root], slice[m]
heapBuild(m, slice, size)
}
}
return
}
func heapSort(slice []int) {
size := len(slice)
//这里是大于等于0
for i := size / 2 - 1; i >= 0; i-- {
heapBuild(i, slice, size)
}
//这里是大于等于1
for last := size - 1; last >= 1; last-- {
slice[0], slice[last] = slice[last], slice[0]
heapBuild(0, slice, last)
}
fmt.Println("堆 排 序", slice)
return
}
/*
TODO:冒泡排序
*/
func bubbleSort(slice []int) {
size := len(slice)
for i := 0; i < size - 1; i++ {
for j := 0; j < size - 1 - i; j++ {
if slice[j] > slice[j + 1] {
slice[j + 1], slice[j] = slice[j], slice[j + 1]
}
}
}
fmt.Println("冒泡排序", slice)
}
/*
TODO:快速排序
*/
func quickSort(slice []int, start int, end int) {
if (start >= end) {
return
}
first, last := start, end
key := slice[first]
for last > first {
for last > first&&slice[last] >= key {
last--
}
slice[first] = slice[last]
for last > first&&slice[first] <= key {
first++
}
slice[last] = slice[first]
}
slice[first] = key
quickSort(slice, start, first - 1)
quickSort(slice, first + 1, end)
}
/*
TODO:归并排序
*/
func merge(slice []int, sliceTemp []int, start, mid, end int) {
i, j, k := start, mid + 1, start
for i <= mid && j <= end {
if slice[i] > slice[j] {
sliceTemp[k] = slice[j]
k++; j++
} else {
sliceTemp[k] = slice[i]
k++; i++
}
}
for i <= mid {
sliceTemp[k] = slice[i]
k++; i++
}
for j <= end {
sliceTemp[k] = slice[j]
k++; j++
}
for i := start; i <= end; i++ {
slice[i] = sliceTemp[i]
}
}
func mergeSort(slice []int, sliceTemp []int, start int, end int) {
if end > start {
mid := (start + end) / 2
mergeSort(slice, sliceTemp, start, mid)
mergeSort(slice, sliceTemp, mid + 1, end)
merge(slice, sliceTemp, start, mid, end)
}
}
/*
TODO:基数排序
*/
func radixSort(slice []int) {
size := len(slice)
maxNumOfDigital := 1
maxRadix := 10
for i := 0; i < size; i++ {
for slice[i] > maxRadix {
maxNumOfDigital++
maxRadix *= 10
}
}
var bucket [10]int
temp := make([]int, size, size)
radix := 1
for i := 0; i < maxNumOfDigital; i++ {
for j := 0; j < 10; j++ {
bucket[j] = 0;
}
for j := 0; j < size; j++ {
k := (slice[j] / radix) % 10
bucket[k]++;
}
for j := 1; j < 10; j++ {
bucket[j] = bucket[j] + bucket[j - 1]
}
for j := size - 1; j >= 0; j-- {
k := (slice[j] / radix) % 10
temp[bucket[k] - 1] = slice[j]
bucket[k]--
}
for j := 0; j < size; j++ {
slice[j] = temp[j]
}
radix *= 10
}
fmt.Println("基数排序", slice)
}
func main() {
slice := []int{111, 2, 3, 100, 2, 4, 5, 100}
sliceTemp := make([]int, len(slice), len(slice))
copy(sliceTemp, slice)
straightInsertionSort(sliceTemp)
copy(sliceTemp, slice)
shellSort(sliceTemp)
copy(sliceTemp, slice)
selectSort(sliceTemp)
copy(sliceTemp, slice)
heapSort(sliceTemp)
copy(sliceTemp, slice)
bubbleSort(sliceTemp)
copy(sliceTemp, slice)
quickSort(sliceTemp, 0, len(sliceTemp) - 1)
fmt.Println("快速排序", sliceTemp)
copy(sliceTemp, slice)
sliceTemp2 := make([]int, len(slice), len(slice))
mergeSort(sliceTemp, sliceTemp2, 0, len(slice) - 1)
fmt.Println("归并排序", sliceTemp)
copy(sliceTemp, slice)
radixSort(sliceTemp)
}
【day-15】golang各种排序
最新推荐文章于 2024-09-01 19:51:00 发布