Go to implement bubble sort, select sort, quick sort, and insert Sort method _golang

Source: Internet
Author: User

The examples in this article describe the way the go language implements bubble sort, select sort, fast sort, and insert ordering. Share to everyone for your reference. The specific analysis is as follows:

The algorithm is the soul of the program, and the sorting algorithm is the most basic algorithm. There are a number of sorting algorithms, here are 4 sorting algorithms: Bubble sort, select sort, fast sort and insert sort, to small to large for example.

First, bubble sort

The principle of bubble sorting is to iterate over the given array multiple times, each time comparing the next two numbers, if the previous one is larger than the latter, 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;

Copy Code code as follows:
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] {
Exchange
NUMS[J], nums[j-1] = nums[j-1], nums[j]
}
}
}
}

Second, select the sort

The principle of selecting a sort is to iterate over the given array multiple times, each time finding the index of the largest value.

Copy Code code as follows:
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 the largest number and save the 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 fast sorting is to first find a number pivot the array ' average ' into two groups, so that all the numbers in one group is greater than the number in the other group, the position of pivot in the array is its correct position. You then do this again for both sets of arrays.

Copy Code code as follows:
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)
Recursionsort (Nums, pivot+1, right)
}
}

Func partition (Nums []int, left int, right int) int {
For left < right {
For left < right && Nums[left] <= Nums[right] {
right--
}
If left < right {
Nums[left], nums[right] = Nums[right], Nums[left]
left++
}

For left < right && Nums[left] <= Nums[right] {
left++
}
If left < right {
Nums[left], nums[right] = Nums[right], Nums[left]
right--
}
}
Return to left
}

iv. Insert Sort

The principle of insertion sort is to iterate from the second number to the right, each time moving the element of that position to the left and placing it in a correct position (larger than the left and smaller than the right).

Copy Code code as follows:
Insert sort (sort 10,000 integers, spents about 30ms)
Func Insertsort (nums []int) {
For i: = 1; I < Len (nums); i++ {
If nums[i] < Nums[i-1] {
J: = I-1
Temp: = Nums[i]
For j >= 0 && nums[j] > Temp {
NUMS[J+1] = Nums[j]
j--
}
NUMS[J+1] = Temp
}
}
}

Multiple tests have shown that fast sorting is the most efficient.

I hope this article will help you with your go language program.

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.