The algorithm is the soul of the program, and the sorting algorithm is the most basic algorithm. There are many kinds of sorting algorithms, this article introduces 4 sorting algorithms: Bubble sort, select sort, quick sort and insert sort, take small to large as an example.
First, bubble sort
The bubble sort principle is that the given array is traversed several times, each time comparing the adjacent two numbers, if the previous one is larger than the next, then the two numbers are exchanged. After the first traversal, the largest number is on the far right, and after the second traversal, the second largest number is in the second position of the right;
Bubble sort (sort 10,000 random integers, spents about 145ms) func bubblesort (nums []int) {for i: = 0; i < len (nums); i++ {for J: = 1; j < Len (nums)- I J + + {if NUMS[J] < Nums[j-1] {//Interchange nums[j], nums[j-1] = nums[j-1], Nums[j]}}}
Second, choose the sort
The principle of selecting sorting is to iterate through the given array multiple times, identifying the index of the largest value at a time.
Select sort (sort 10,000 random integers, spents about 45ms) func selectsort (nums []int) {Length: = Len (nums) for I: = 0; i < length; i++ {maxindex: = 0/ /Find maximum one number, save index value for J: = 1; J < Length-i; J + + {if nums[j] > Nums[maxindex] {maxindex = j}}nums[length-i-1], Nums[maxindex] = Nums[maxindex], nums[length-i-1]}}
Three, quick sort
The principle of quick sorting is to first find a number pivot to divide the array ' average ' into two groups, so that all the numbers in one group are greater than the number in another group, where the position of pivot in the array is its correct position. This is then done again for these two sets of arrays.
Quick sort (sort 10,000 random integers, spents about 0.9ms) func quickSort (nums []int) {recursionsort (nums, 0, Len (nums)-1)}func Recursionsort (nums []int, left int, right int] {if left < right {pivot: = partition (Nums, left, right) Recursionsort (Nums, left, pivot-1) re Cursionsort (Nums, pivot+1, right)}}func partition (Nums []int, left int, right int) int {To left < right {for Left < Right && Nums[left] <= nums[right "{Right--}if left < right {Nums[left], nums[right] = Nums[right], Nums[le Ft]left++}for left < right && Nums[left] <= nums[right] {Left++}if Left < right {Nums[left], nums[right] = Nums[right], Nums[left]right--}}return left}
Iv. Insert Sort
The insertion sort principle is to iterate from the second number to the right, each time moving the elements of that position to the left, placed in the correct position (larger than the left, smaller than the right).
Insert sort (sort 10,000 integers, spents approximately 30ms) func insertsort (nums []int) {for i: = 1; i < Len (nums); i++ {if nums[i] < nums[i-1] {j: = I-1temp: = nums[i]for J >= 0 && nums[j] > Temp {nums[j+1] = nums[j]j--}nums[j+1] = temp}}}
With multiple tests, it can be found that fast sequencing is the most efficient.
Sorting algorithm in detail (go language Implementation): Bubble sort/Select sort/Quick sort/Insert Sort