排序算法、链表反转

快速排序、冒泡排序、选择排序

链表反转

package main

import (
    "fmt"
)

type Node struct {
    Data int
    Next *Node
}

type TreeNode struct {
    Data  int
    Left  *TreeNode
    Right *TreeNode
}

/**中序遍历**/
func (tree *TreeNode) ViewMiddle() {

    if tree.Left != nil {
        tree.Left.ViewMiddle()
    }

    fmt.Println(tree.Data)

    if tree.Right != nil {
        tree.Right.ViewMiddle()
    }

}

func (tree *TreeNode) ViewPrefix() {

    fmt.Println(tree.Data)

    if tree.Left != nil {
        tree.Left.ViewMiddle()
    }

    if tree.Right != nil {
        tree.Right.ViewMiddle()
    }

}

func (tree *TreeNode) InsertNode(addNode *TreeNode) bool {

    if addNode.Data > tree.Data {
        if tree.Right != nil {
            return tree.Right.InsertNode(addNode)
        }
        tree.Right = addNode
        return true
    }

    if addNode.Data < tree.Data {
        if tree.Left != nil {
            return tree.Left.InsertNode(addNode)
        }
        tree.Left = addNode
        return true
    }

    return false
}

func main() {

    list := []int{2443, 6472, 36723, 3672, 7, 63, 455, 748347834, 535, 455, 12, 23, 987,
        648764, 647364, 4628764, 648364, 6464, 8736, 132, 464, 535, 1}
    //head := GenLinkList(list)
    //head = ReverseLinkList2(head)
    //PrintLinkList(head)
    tree := GenTree(list)
    tree.ViewPrefix()
    fmt.Println(tree)
}

func GenTree(list []int) *TreeNode {
    root := &TreeNode{
        Data: list[0],
    }

    for _, data := range list[1:] {
        root.InsertNode(&TreeNode{
            Data: data,
        })
    }
    return root
}

func ReverseLinkList2(headNode *Node) *Node {

    curNode := headNode.Next
    preNode := headNode

    for {
        if curNode.Next != nil {
            nextCurNode := curNode.Next
            curNode.Next = preNode
            preNode = curNode
            curNode = nextCurNode
        } else {
            curNode.Next = preNode
            break
        }

    }
    headNode.Next = nil

    return curNode

}

/**翻转链表 - 递归**/
func ReverseLinkList(headNode *Node) *Node {

    if headNode.Next == nil {
        return headNode
    }

    nextNode := headNode.Next
    newHead := ReverseLinkList(nextNode)

    nextNode.Next = headNode
    headNode.Next = nil

    return newHead
}

/**链表生成**/
func GenLinkList(list []int) *Node {
    headNode := &Node{
        Data: list[0],
    }
    curNode := headNode
    for i, data := range list {
        if i > 0 {
            curNode.Next = &Node{
                Data: data,
            }
            curNode = curNode.Next
        }
    }
    return headNode
}

func PrintLinkList(headNode *Node) {

    for {

        fmt.Println(headNode.Data)

        if headNode.Next == nil {
            break
        }
        headNode = headNode.Next

    }
}

/**快速排序**/
func QuickSort(list []int, offsetStart, offsetEnd int) []int {

    left := offsetStart
    right := offsetEnd
    start := list[offsetStart]
    for offsetStart != offsetEnd {

        for start <= list[offsetEnd] && offsetStart < offsetEnd {
            offsetEnd--
        }

        for start >= list[offsetStart] && offsetStart < offsetEnd {
            offsetStart++
        }

        if offsetStart < offsetEnd {
            list[offsetStart], list[offsetEnd] = list[offsetEnd], list[offsetStart]
        }
    }

    list[left] = list[offsetStart]
    list[offsetStart] = start

    if offsetStart-left > 1 {
        list = QuickSort(list, left, offsetStart-1)
    }

    if right-offsetStart > 1 {
        list = QuickSort(list, offsetStart+1, right)
    }

    //fmt.Println(list)
    return list
}

/**冒泡排序**/
func BubbleSort(list []int) {

    listSize := len(list)
    for i := 0; i < listSize-1; i++ {
        for j := 0; j < listSize-i-1; j++ {
            if list[j] > list[j+1] {
                list[j+1], list[j] = list[j], list[j+1]
            }
        }
        fmt.Println(list)
    }

    fmt.Println(list)
}

/**选择排序**/
func SelectSort(list []int) {

    listSize := len(list)
    for i := 0; i < listSize-1; i++ {
        minIndex := i
        for j := i + 1; j < listSize; j++ {
            if list[j] < list[minIndex] {
                minIndex = j
            }
        }
        list[i], list[minIndex] = list[minIndex], list[i]
        fmt.Println(list)
    }

    fmt.Println(list)
}

你可能感兴趣的:(排序算法、链表反转)