golang 实现常见排序算法

冒泡排序
package main

import "fmt"
import "golang.org/x/exp/constraints"

func main() {
	testArray := make([]int, 0, 10)
	testArray = append(testArray, 1)
	testArray = append(testArray, 6)
	testArray = append(testArray, 5)
	testArray = append(testArray, 7)
	testArray = append(testArray, 4)
	testArray = append(testArray, 4)
	testArray = append(testArray, 8)
	testArray = append(testArray, 3)
	testArray = append(testArray, 2)
	testArray = append(testArray, 9)

	bubbleSort(testArray)

	fmt.Printf("排序后的数组为:%+v", testArray)
}

func bubbleSort[T constraints.Ordered](data []T) {
	length := len(data)
	for i := 0; i < length; i++ {
		for j := i; j < length; j++ {
			if data[i] > data[j] {
				data[i], data[j] = data[j], data[i]
			}
		}
	}
}

选择排序
package main

import "fmt"
import "golang.org/x/exp/constraints"

func main() {
	testArray := make([]int, 0, 10)
	testArray = append(testArray, 1)
	testArray = append(testArray, 6)
	testArray = append(testArray, 5)
	testArray = append(testArray, 7)
	testArray = append(testArray, 4)
	testArray = append(testArray, 4)
	testArray = append(testArray, 8)
	testArray = append(testArray, 3)
	testArray = append(testArray, 2)
	testArray = append(testArray, 9)

	selectSort(testArray)

	fmt.Printf("排序后的数组为:%+v\n", testArray)

	intArray := []int{4, 5, 6, 7, 8, 3, 2, 1}

	selectSort(intArray)

	fmt.Printf("排序后的数组为:%+v", intArray)
}

func selectSort[T constraints.Ordered](data []T) {
	length := len(data)
	for i := 0; i < length; i++ {
		minNum := data[i]
		minKey := i
		for j := i + 1; j < length; j++ {
			if minNum > data[j] {
				minNum = data[j]
				minKey = j
			}
		}
		data[i], data[minKey] = data[minKey], data[i]
	}
}

插入排序
package main

import "fmt"
import "golang.org/x/exp/constraints"

func main() {
	testArray := make([]int, 0, 10)
	testArray = append(testArray, 1)
	testArray = append(testArray, 6)
	testArray = append(testArray, 5)
	testArray = append(testArray, 7)
	testArray = append(testArray, 4)
	testArray = append(testArray, 4)
	testArray = append(testArray, 8)
	testArray = append(testArray, 3)
	testArray = append(testArray, 2)
	testArray = append(testArray, 9)

	insertSort(testArray)

	fmt.Printf("排序后的数组为:%+v\n", testArray)

	intArray := []int{4, 5, 6, 7, 8, 3, 2, 1}

	insertSort(intArray)

	fmt.Printf("排序后的数组为:%+v", intArray)
}

func insertSort[T constraints.Ordered](data []T) {
	length := len(data)
	for i := 1; i < length; i++ {
		if data[i-1] > data[i] {
			temp := data[i]
			// 找到插入的位置 比如 1342 42这里 只需要从4往前找到它应该插入的位置
			j := i
			for ; j > 0; j-- {
				if data[j-1] > temp {
					data[j] = data[j-1]
				} else {
					break
				}

			}
			data[j] = temp
		}

	}
}

归并排序
package main

import "fmt"

func main() {
	intArray := []int{4, 5, 6, 7, 8, 3, 2, 1}
	intArray = mergeSort(intArray[:])
	fmt.Printf("%v", intArray)
}

func mergeSort(arrayData []int) []int {
	length := len(arrayData)
	if length <= 1 {
		return arrayData
	}

	count := length / 2

	left := mergeSort(arrayData[:count])
	right := mergeSort(arrayData[count:])

	return merge(left, right)
}

func merge(left []int, right []int) []int {
	i, j := 0, 0
	leftLength := len(left)
	rightLength := len(right)
	var res []int

	//
	for {
		if i >= leftLength || j >= rightLength {
			break
		}
		if left[i] <= right[j] {
			res = append(res, left[i])
			i++
		} else {
			res = append(res, right[j])
			j++
		}
	}

	if i < leftLength {
		for ; i < leftLength; i++ {
			res = append(res, left[i])
		}
	}

	if j < rightLength {
		for ; j < rightLength; j++ {
			res = append(res, right[j])
		}
	}
	return res
}

快排
package main

import "fmt"


func main() {
	intArray := []int{4, 5, 6, 7, 3, 2, 1, 8, 9}
	intArray2 := []int{4, 5, 6, 7, 8, 3, 2, 1}
	intArray3 := []int{2, 1}
	quickSort(intArray[:], 0, len(intArray)-1)
	quickSort(intArray2, 0, len(intArray2)-1)
	quickSort(intArray3, 0, len(intArray3)-1)
	fmt.Printf("%v\n", intArray)
	fmt.Printf("%v\n", intArray2)
	fmt.Printf("%v", intArray3)
}

func quickSort(arrayData []int, start, end int) {
	if start >= end {
		return
	}

	pivot := quickSortDo(arrayData, start, end)

	quickSort(arrayData, start, pivot-1)
	quickSort(arrayData, pivot+1, end)
}

// 将当前区间 以end 为标准 分成 前后2个区间 前面的区间小于 end ,后面的end 大于end
func quickSortDo(data []int, start, end int) int {
	// 定义初始位置
	startSmall, startLarge := start, start
	for startLarge < end {
		// startSmall, startLarge 2个标识 分别标识 小于 end 的 和大于end 的位置
		if data[startLarge] < data[end] {
			if startSmall != startLarge {
				data[startSmall], data[startLarge] = data[startLarge], data[startSmall]
			}
			startSmall++
		}
		startLarge++
	}
	data[startSmall], data[end] = data[end], data[startSmall]
	// startSmall 相当于标识一个中间数
	return startSmall
}

你可能感兴趣的:(golang,算法,golang,排序算法,算法)