十大排序算法
1、选择排序:
package main
import "fmt"
func selectionSort(arr []int) {
if arr == nil || len(arr) < 2 {
return
}
n := len(arr)
for i := 0; i < n-1; i++ {
minindex := i
for j := i + 1; j < n; j++ {
if arr[j] < arr[minindex] {
minindex = j
}
}
swap(arr, i, minindex)
}
}
func swap(arr []int, i int, j int) {
tmp := arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
func main() {
arr := []int{1, 2, 5, 23, 0, 42, 8, 12}
fmt.Println(arr)
selectionSort(arr)
fmt.Println(arr)
}
2、插入排序:
package main
import (
"fmt"
)
func insertSort(arr []int) {
if arr == nil || len(arr) < 2 {
return
}
n := len(arr)
for i := 1; i < n; i++ {
for j := i; j > 0; j-- {
if arr[j-1] > arr[j] {
swap(arr, j-1, j)
}
}
}
}
func swap(arr []int, i int, j int) {
tmp := arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
func main() {
arr := []int{1, 4, 2, 5, 7, 0}
fmt.Println(arr)
insertSort(arr)
fmt.Println(arr)
}
3、冒牌排序:
package main
import "fmt"
func maopao(arr []int) {
if arr == nil || len(arr) < 2 {
return
}
n := len(arr)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if arr[j] < arr[i] {
swap(arr, i, j)
}
}
}
}
func swap(arr []int, i int, j int) {
tmp := arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
func main() {
arr := []int{1, 4, 2, 8, 7, 6}
fmt.Println(arr)
maopao(arr)
fmt.Println(arr)
}
4、希尔排序:
package main
import (
"fmt"
"log"
)
func shellSort(arr []int) {
count := len(arr)
fmt.Println(count)
for d := len(arr) / 2; d > 0; d /= 2 {
for i := d; i < len(arr); i++ {
for j := i; j >= d; j -= d {
log.Println(j-d, j)
if arr[j-d] > arr[j] {
arr[j], arr[j-d] = arr[j-d], arr[j]
}
}
}
}
}
func myselfShellSort(arr []int) {
count := len(arr)
log.Println(count)
for h := len(arr) / 2; h > 0; h /= 2 {
for i := h; i < count; i++ {
for j := i; j >= h; j -= h {
if arr[j-h] > arr[j] {
arr[j], arr[j-h] = arr[j-h], arr[j]
}
}
}
}
}
func main() {
arr := []int{6, 5, 1, 7, 4, 8, 9, 3, 2}
myselfShellSort(arr)
log.Println(arr)
arr1 := []int{2, 3}
log.Println(arr1)
arr1[0], arr[1] = arr1[1], arr1[0]
log.Println(arr1)
}
5、归并排序:
package main
import "log"
func guiBinSort(arr []int) []int {
lenArr := len(arr)
if lenArr <= 1 {
return arr
}
mid := len(arr) / 2
log.Println(mid)
leftArr := guiBinSort(arr[:mid])
rightArr := guiBinSort(arr[mid:])
return merge(leftArr, rightArr)
}
func merge(leftArr []int, rightArr []int) []int {
leftIndex, rightIndex, mergeArr := 0, 0, []int{}
for leftIndex < len(leftArr) && rightIndex < len(rightArr) {
if leftArr[leftIndex] <= rightArr[rightIndex] {
mergeArr = append(mergeArr, leftArr[leftIndex])
leftIndex += 1
} else {
mergeArr = append(mergeArr, rightArr[rightIndex])
rightIndex += 1
}
}
if leftIndex < len(leftArr) {
mergeArr = append(mergeArr, leftArr[leftIndex:]...)
}
if rightIndex < len(rightArr) {
mergeArr = append(mergeArr, rightArr[rightIndex:]...)
}
return mergeArr
}
func main() {
arr := []int{6, 5, 1, 7, 4, 8, 9, 3, 2}
arr = guiBinSort(arr)
log.Println(arr)
}
6、快速排序:
package main
import (
"log"
"math/rand"
)
func quickSort(arr []int, leftIndex, rightIndex int) []int {
if leftIndex < rightIndex {
pivot := partion(arr, leftIndex, rightIndex)
quickSort(arr, leftIndex, pivot-1)
quickSort(arr, pivot+1, rightIndex)
}
log.Println(arr)
return arr
}
func partion(arr []int, leftIndex, rightIndex int) int {
i := leftIndex
j := rightIndex
pivot := arr[leftIndex]
for i != j {
for i < j && arr[j] > pivot {
j -= 1
}
for i < j && arr[i] <= pivot {
i += 1
}
if i < j {
arr[i], arr[j] = arr[j], arr[i]
}
}
arr[leftIndex], arr[i] = arr[i], arr[leftIndex]
return i
}
func main() {
arr := []int{}
for i := 0; i <= 10; i++ {
arr = append(arr, rand.Intn(14))
}
log.Println(arr)
b := quickSort(arr, 0, len(arr)-1)
log.Println(b)
}
7、堆排序:
package main
import (
"log"
"math/rand"
)
func heapify(arr []int, low, hight int) {
i := low
j := low*2 + 1
temp := arr[i]
for j <= hight {
if j+1 <= hight && arr[j] < arr[j+1] {
j += 1
}
if arr[j] > temp {
arr[i] = arr[j]
i = j
j = 2*i + 1
} else {
break
}
}
arr[i] = temp
}
func heapSort(arr []int) []int {
n := len(arr)
for i := n/2 - 1; i > -1; i-- {
log.Println("this is i", i)
heapify(arr, i, n-1)
}
for hight := n - 1; hight > -1; hight-- {
log.Println("hight", hight)
arr[0], arr[hight] = arr[hight], arr[0]
heapify(arr, 0, hight-1)
}
return arr
}
func main() {
arr := []int{}
for i := 0; i < 10; i++ {
arr = append(arr, rand.Intn(100))
}
log.Println(arr)
arr = heapSort(arr)
log.Println(arr)
}
8、桶排序:
package main
import (
"log"
"math/rand"
"sort"
)
func tongSort(arr []int) []int {
if len(arr) == 0 {
return []int{}
}
// 1.找到最大值和最小值
min := arr[0]
max := arr[0]
for _, element := range arr {
if element > max {
max = element
}
if element < min {
min = element
}
}
// 2构建技术捅
defaultBuckets := 2
bucketCount := (max-min)/defaultBuckets + 1
buckets := make([][]int, bucketCount)
for i := 0; i < bucketCount; i++ {
buckets[i] = make([]int, 0)
}
// 3 将元素放到计数捅内计数
for _, element := range arr {
i := (element - min) / defaultBuckets
buckets[i] = append(buckets[i], element)
}
// 4.遍历计数捅 取出元数排序
arrIndex := 0
for _, buckets := range buckets {
// 捅内排序
sort.SliceIsSorted(buckets, func(i, j int) bool {
return buckets[i] < buckets[j]
})
for _, elemet := range buckets {
arr[arrIndex] = elemet
arrIndex++
}
}
return arr
}
func main() {
arr := []int{}
for i := 0; i < 10; i++ {
arr = append(arr, rand.Intn(100))
}
log.Println(arr)
arr = tongSort(arr)
log.Println(arr)
}
9、计数排序:
package main
import (
"log"
"math/rand"
)
func countingSort(arr []int) []int {
if len(arr) == 0 {
log.Println("")
return []int{}
}
max := arr[0]
for _, element := range arr {
if element > max {
max = element
}
}
buckets := make([]int, max+1)
for _, element := range arr {
buckets[element] += 1
}
arrIndex := 0
for element, elementNum := range buckets {
for elementNum > 0 && arrIndex < len(arr) {
arr[arrIndex] = element
elementNum--
arrIndex++
}
}
return arr
}
func main() {
arr := []int{}
for i := 0; i < 10; i++ {
arr = append(arr, rand.Intn(100))
}
log.Println(arr)
arr = countingSort(arr)
log.Println(arr)
}
10、基数排序:
package main
import (
"fmt"
"log"
"math/rand"
"sort"
)
func RadixSort(arr []int) []int {
if len(arr) == 0 {
fmt.Printf("%s\n", "err: len(arr) == 0")
return []int{}
}
defaultRadix := 10
defaultBucketsCount := 10
buckets := make([][]int, defaultBucketsCount)
for i := 0; i < defaultBucketsCount; i++ {
buckets[i] = make([]int, 0)
}
for _, element := range arr {
i := element / defaultRadix
if i > defaultBucketsCount {
i = defaultBucketsCount - 1
} else if i < 0 {
i = 0
}
buckets[i] = append(buckets[i], element)
}
arrIndex := 0
for _, bucket := range buckets {
sort.Slice(bucket, func(i, j int) bool {
return bucket[i] < bucket[j]
})
for _, element := range bucket {
arr[arrIndex] = element
arrIndex++
}
}
return arr
}
func main() {
arr := []int{}
for i := 0; i < 10; i++ {
arr = append(arr, rand.Intn(100))
}
log.Println(arr)
arr = RadixSort(arr)
log.Println(arr)
}
11、桶排序:
package main
import (
"log"
"math/rand"
)
/*
木桶排序
木桶排序的核心思想
关键词:分桶
将数组分到有限数量的桶子里,每个桶子再个别排序。
其实木桶排序和基数排序很相似,区别在于基数排序需要多趟桶排序,并且记录当前排序的结果。
*/
func selectMax(arr []int) (min, max int) {
if len(arr) == 0 {
return 0, 0
}
min = arr[0]
max = arr[1]
for i := 0; i < len(arr); i++ {
if arr[i] > arr[0] {
max = arr[i]
}
if arr[i] < arr[0] {
min = arr[i]
}
}
return min, max
}
func selectSort(arr []int) []int {
// 复习选择排序
lenth := len(arr)
if lenth <= 1 {
return arr
}
for i := 0; i < lenth-1; i++ {
min := 1
for j := i + 1; j < lenth; j++ {
if arr[min] > arr[j] {
min = j
}
}
if i != min {
arr[i], arr[min] = arr[min], arr[i]
}
}
return arr
}
func buckSort(arr []int) []int {
length := len(arr)
if length <= 1 {
return arr
}
num := length // 长度
min, max := selectMax(arr)
log.Println(max, min)
index := 0
buckets := make([][]int, num)
for i := 0; i < length; i++ {
index = arr[i] * (num - 1) / max
buckets[index] = append(buckets[index], arr[i])
}
log.Println(buckets)
// 木桶排序
tmppose := 0
for i := 0; i < num; i++ {
bucketslen := len(buckets[i])
if bucketslen > 0 {
buckets[i] = selectSort(buckets[i])
copy(arr[tmppose:], buckets[i])
tmppose += bucketslen
}
}
return arr
}
func main() {
arr := []int{}
for i := 0; i < 10; i++ {
arr = append(arr, rand.Intn(100))
}
log.Println(arr)
arr = buckSort(arr)
log.Println(arr)
}