The sort algorithm of Go basic learning Seven

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In this section, you will learn the classic sort algorithms for the go language, such as insert sort, select sort, bubble sort, hill sort, merge sort, heap sort and fast row, binary search, external sort, and mapreduce.

First, the classical sorting algorithm

Algorithm learning is very important, it is an important criterion to test the level of a programmer. Learning algorithms can not be memorized, need to understand the ideas, so as to be flexible to apply to the actual development.

Seven Classic sorting algorithms

    • Insert Sort
    • Select sort
    • Bubble sort
    • Hill sort
    • Merge sort
    • Heap Sort
    • Quick Sort

Second, insert sort

Consider first the question: for arrays of length n, the first n-1 bits are ascending and orderly, how are they sorted?

Specific implementation methods:

package mainimport "fmt"  func insertionSort(arr []int) {     for i := 1; i < len(arr); i++ {         value := arr[i]         for j := i - 1; j >= 0; j-- {             if value < arr[j] {                 arr[j+1], arr[j] = arr[j], value             } else {                 break             }         }     }}func main() {    arr := []int{6, 5, 4, 3, 2, 1, 0}    insertionSort(arr)    fmt.Println("Sorted arr: ", arr)}

Reference article: "Bat background Getting Started" lesson two: arrays and sorting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.