package main
import "fmt"
func Bubble(num []int) []int{
for i:= 0; i < len(num) - 1; i++{
for j := 0; j < len(num) - 1 - i; j++{
if num[j] > num[j+1]{
t := num[j]
num[j] = num[j+1]
num[j+1] = t
}
}
}
return num
}
func Select(num []int) []int{
for i := 0; i < len(num) -1 ; i++{
max := i
for j := i + 1; j < len(num); j++{
if num[i] > num[j]{
max = j
}
}
if max != i{
t := num[max]
num[max] = num[i]
num[i] = t
}
}
return num
}
func Patition(a []int, left, right int) int{
p := a[left]
for left < right{
for left < right && p <= a[right]{
right--
}
a[left] = a[right]
for left < right && p >= a[left]{
left++
}
a[right] = a[left]
}
a[left] = p
return left
}
func Quick(num []int, left, right int){
if left < right{
m := Patition(num, left, right)
Quick(num, 0, m-1)
Quick(num, m+1, right)
}
}
func InsertSort(nums []int) []int{
for i := 1; i < len(nums); i++{ //假设第一个元素是有序的,下标从1开始,是需要插入的元素
for j := 0; j < i; j++{//从0开始跟有序队列进行比较,保证前面的元素都有序
if nums[i] < nums[j]{
min := nums[i]
nums[i] = nums[j]
nums[j] = min
}
}
}
return nums
}
func main(){
num := []int{10,9,8,2,7,6,5,4,3,2}
/*
fmt.Println("Bubble:", Bubble(num))
num1 := []int{10,9,8,2,7,6,5,4,3,2,1}
fmt.Println("Select:", Select(num1))
num0 := []int{10,9,8,2,7,6,5,4,3,2,0,8}
Quick(num0, 0, len(num0) - 1)*/
num0 := InsertSort(num)
fmt.Println("Quick:", num0)
/*
for i, j := 0, len(nums) - 1; i < j; i, j = i + 1, j - 1{
if nums[i] + nums[j] == target{
a
}
}*/
}