排序算法

package main

import (
	"fmt"
	"strconv"
)

func main() {
	arr := []int{12, 34, 651, 3, 65, 1, 4}
	fmt.Println(arr)
	// 冒泡排序
	// value := bubble_sort(arr)
	// 选择排序
	// value := selection_sort(arr)
	// 插入排序
	// value := insertion_sort(arr)
	// 快速排序
	// value := quick_sort(arr)
	// 归并排序
	// value := merge_sort(arr)
	// 希尔排序
	// value := shell_sort(arr)
	// 计数排序
	// value := count_sort(arr)
	// 桶排序
	// 基数排序
	value := radix_sort(arr)
	fmt.Println(value)
}

// 冒泡排序
func bubble_sort(arr []int) []int {
	len := len(arr)
	for i := 0; i < len-1; i++ {
		for j := 0; j < len-i-1; j++ {
			if arr[j] > arr[j+1] {
				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
	return arr
}

// 选择排序
func selection_sort(arr []int) []int {
	len := len(arr)
	for i := 0; i < len-1; i++ {
		for j := i + 1; j < len; j++ {
			if arr[i] > arr[j] {
				arr[i], arr[j] = arr[j], arr[i]
			}
		}
	}
	return arr
}

// 插入排序
func insertion_sort(arr []int) []int {
	len := len(arr)
	// fmt.Println(len)
	for i := 1; i < len; i++ {
		if arr[i] < arr[i-1] {
			a := arr[i]
			arr[i] = arr[i-1]
			j := i - 1 - 1
			for {
				if a >= arr[j] {
					break
				}
				arr[j+1] = arr[j]
				j = j - 1
				if j < 0 {
					break
				}
			}
			arr[j+1] = a
		}
	}
	return arr
}

// 快速排序
func quick_sort(arr []int) []int {
	len := len(arr)
	if len < 2 {
		return arr
	}
	key := arr[0]
	left := make([]int, 0)
	right := make([]int, 0)
	for i := 1; i < len; i++ {
		if arr[i] < key {
			left = append(left, arr[i])
		} else {
			right = append(right, arr[i])
		}
	}
	left = quick_sort(left)
	right = quick_sort(right)

	return append(append(left, key), right...)
}

// 归并排序
func merge_sort(arr []int) []int {
	len := len(arr)
	if len < 2 {
		return arr
	}
	m := len / 2
	// fmt.Println(m)
	left := arr[:m]
	right := arr[m:]
	// fmt.Println(left)
	// fmt.Println(right)
	left = merge_sort(left)
	right = merge_sort(right)
	return merge(left, right)
}
func merge(left, right []int) []int {
	res := make([]int, 0)
	for len(left) > 0 && len(right) > 0 {
		if left[0] > right[0] {
			res = append(res, right[0])
			right = right[1:]
		} else {
			res = append(res, left[0])
			left = left[1:]
		}
	}
	for len(left) > 0 {
		res = append(res, left[0])
		left = left[1:]
	}
	for len(right) > 0 {
		res = append(res, right[0])
		right = right[1:]
	}
	return res
}

// 希尔排序
func shell_sort(arr []int) []int {
	len := len(arr)
	f := 3
	h := 1
	for h < len/f {
		h = f*h + 1
	}
	// fmt.Println(h)
	for h >= 1 {
		// fmt.Println("h = ", h)
		for i := h; i < len; i++ {
			// fmt.Println(i)
			for j := i; j >= h; j -= h {
				// fmt.Println("j = ", j)
				if arr[j] < arr[j-h] {
					arr[j], arr[j-h] = arr[j-h], arr[j]
				}

			}
		}
		h = h / f
	}
	return arr
}

// 计数排序
func count_sort(arr []int) []int {
	len := len(arr)
	if len < 2 {
		return arr
	}
	max := arr[0]
	for i := 1; i < len; i++ {
		if arr[i] > max {
			max = arr[i]
		}
	}
	res := make([]int, max+1)
	// fmt.Println(res)
	for _, v := range arr {
		res[v] = res[v] + 1
	}
	NewArr := make([]int, 0)
	for k, v := range res {
		for i := 0; i < v; i++ {
			NewArr = append(NewArr, k)
		}
	}
	return NewArr
}

// 基数排序
func radix_sort(arr []int) []int {
	kl := make([][]int, 10)
	// fmt.Println(kl)
	// lenArr := len(arr)
	max := arr[0]
	for _, v := range arr {
		if max < v {
			max = v
		}
	}
	str := strconv.Itoa(max)
	lenStr := len(str)
	// fmt.Println(lenStr)
	for i := 0; i < lenStr; i++ {
		for _, v := range arr {
			strV := strconv.Itoa(v)
			lenV := len(strV)
			if lenV > i {
				k := strV[lenV-i-1 : lenV-i]
				key, _ := strconv.Atoi(k)
				kl[key] = append(kl[key], v)
			} else {
				kl[0] = append(kl[0], v)
			}

		}
		// fmt.Println(kl)
		arr = arr[0:0]
		for k, v := range kl {
			arr = append(arr, v...)
			kl[k] = kl[k][0:0]
		}
	}

	return arr
}

相关推荐