常见数据结构和算法(golang)

arr := [...]int{15,23,8,10,7,2,6,1,3,5}
 // 冒泡排序
 flag := true
 for i:=0;i<len(arr)-1;i++{
 	for j:=0;j<len(arr)-1-i;j++{
 		if arr[j] > arr[j+1]{
 			arr[j],arr[j+1] = arr[j+1],arr[j]
 			flag = false
		}
	}
 	if flag == true{
 		break
	}
 }
 fmt.Println(arr)
//插入排序
 for i:=0;i<len(arr);i++{
 	for j:=i;j>0;j--{
 		if arr[j] < arr[j-1]{
 			arr[j], arr[j-1] = arr[j-1], arr[j]
		}
	}
 }
 fmt.Println(arr)
// 选择排序
temp := 0
for i:=0;i<len(arr);i++{
	temp = i
	for j:=i;j<len(arr);j++{
		if arr[temp] > arr[j]{
			temp = j
   	}
   }
	arr[temp], arr[i] = arr[i], arr[temp]
}
fmt.Println(arr)
// 快速排序
func fast(arr []int, low, high int){
   if low >= high{
   	return
   }
   start := low
   end := high
   mid := arr[start]
   for start < end{
   	for (start < end) && (arr[end] >= mid){
   		end--
   	}
   	arr[start] = arr[end]
   	for (start < end) && (arr[start] < mid){
   		start++
   	}
   	arr[end] = arr[start]
   }
   arr[start] = mid
   fast(arr, low, end-1)
   fast(arr, end+1, high)
}

//归并
func merge_sort(arr []int, p ,r int){
	if p < r{
		q:=(p+r)/2
		merge_sort(arr, p, q)
		merge_sort(arr, q+1, r)
		merge(arr,p,q,r)
	}
}

func merge(a []int, p , q, r int)  {
	n1 := q - p + 1
	n2 := r - q

	Left  := make([]int,n1+1)
	Right := make([]int,n2+1)

	for i := 0; i< n2;i++ {
		Right[i] = a[q+i+1]
	}
	Right[n2] = 9999
	//fmt.Println(Right)
	for i := 0; i < n1; i++ {
		Left[i] = a[i+p]
	}
	Left[n1] = 9999
	// fmt.Println(Left)
	i:=0
	j:=0
	for k:=p;k<r+1;k++ {
		if Left[i] <= Right[j] {
			a[k] = Left[i]
			i++
		}else {
			a[k] = Right[j]
			j++
		}
	}
	//fmt.Println(a)
}
// 归并
func merge(arr []int)[] int{
	if len(arr) == 1{
		return arr
	}
	mid := len(arr) / 2
	left := arr[:mid]
	riget := arr[mid:]

	l := merge(left)
	r := merge(riget)

	res := merge_sort(l, r)

	return res
}

func merge_sort(l, r []int) []int {
	l_index := 0
	r_index := 0

	result := make([]int, len(l)+len(r))
	num := 0
	for l_index < len(l) && r_index < len(r){
		if l[l_index] < r[r_index]{
			result[num] = l[l_index]
			l_index++
		}else {
			result[num] = r[r_index]
			r_index++
		}
		num++
	}
	for _, v := range l[l_index:]{
		result[num] = v
		num++
	}
	for _, v := range r[r_index:]{
		result[num] = v
		num++
	}
	return result
}
// 二分查找
func binarySearch(arr []int, num int) bool {
	low := 0
	high := len(arr) - 1
	for low <= high {
		mid := (high+low) / 2
		if arr[mid] > num{
			high = mid - 1
		}else if arr[mid] < num {
			low = mid + 1
		}else {
			return true
		}
	}
	return false
}

完全二叉树

type Node struct {
	Item interface{}
	Lchild *Node
	Rchild *Node
}

type Tree struct {
	root *Node
}

func (t *Tree)Add(item interface{})  {
	// 添加节点
	node := Node{Item:item}
	if t.root == nil{
		t.root = &node
	}else {
		// 从上到下,在从左到右放子节点,层次遍历用队列
		queue := make([]*Node, 0)
		queue = append(queue, t.root)
		for {
			child := queue[0]
			queue = queue[1:]
			if child.Lchild == nil{
				child.Lchild = &node
				return
			}
			if child.Rchild == nil{
				child.Rchild = &node
				return
			}
			queue = append(queue, child.Lchild)
			queue = append(queue, child.Rchild)
		}
	}
}

func (t * Tree)BreathTravel()  {
	// 广度优先遍历 从上至下 从左到右 用队列
	if t.root == nil{
		return
	}
	queue := make([]*Node, 0)
	queue = append(queue, t.root)

	for len(queue) > 0{
		node := queue[0]
		queue = queue[1:]
		fmt.Print(node.Item, "\t")
		if node.Lchild != nil{
			queue = append(queue, node.Lchild)
		}
		if node.Rchild != nil{
			queue = append(queue, node.Rchild)
		}
	}
}

func (t *Tree)DepthTravel()  {
	// 深度优先遍历
	t.postOrder(t.root)
}

func (t *Tree)postOrder(node *Node)  {
	// 后序遍历
	if node == nil{
		return
	}
	t.postOrder(node.Lchild)
	t.postOrder(node.Rchild)
	fmt.Print(node.Item, "\t")
}

func (t *Tree)inOrder(node *Node)  {
	// 中序遍历
	if node == nil{
		return
	}
	t.inOrder(node.Lchild)
	fmt.Print(node.Item, "\t")
	t.inOrder(node.Rchild)
}

func (t *Tree)preOrder(node *Node)  {
	if node == nil{
		return
	}
	fmt.Print(node.Item, "\t")
	t.preOrder(node.Lchild)
	t.preOrder(node.Rchild)
}

堆排序

func heapSort(arr []int) []int {
        arrLen := len(arr)
        buildMaxHeap(arr, arrLen)
        for i := arrLen - 1; i >= 0; i-- {
                swap(arr, 0, i)
                arrLen -= 1
                heapify(arr, 0, arrLen)
        }
        return arr
}

func buildMaxHeap(arr []int, arrLen int) {
        for i := arrLen / 2; i >= 0; i-- {
                heapify(arr, i, arrLen)
        }
}

func heapify(arr []int, i, arrLen int) {
        left := 2*i + 1
        right := 2*i + 2
        largest := i
        if left < arrLen && arr[left] > arr[largest] {
                largest = left
        }
        if right < arrLen && arr[right] > arr[largest] {
                largest = right
        }
        if largest != i {
                swap(arr, i, largest)
                heapify(arr, largest, arrLen)
        }
}

func swap(arr []int, i, j int) {
        arr[i], arr[j] = arr[j], arr[i]
}

你可能感兴趣的:(数据结构和算法,golang,经验分享)