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