In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how Golang realizes common sorting algorithms". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how Golang realizes common sorting algorithms" can help you solve the problem.
Comparison of five basic sorting algorithms
Comparison of five basic sorting algorithms
1. Bubble sorting
algorithmic descriptions
Compare adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair, so that the last element should be the largest.
Repeat for all elements except the last one.
Repeat steps 1 - 3 until sorting is complete.
code demonstrates
func bubbleSort(arr []int) []int { if len(arr) 0; e-- { for i := 0; i
< e; i++ { if arr[i] >arr[i+1] { Swap(arr, i, i+1) //swap elements } } } return arr}func Swap(arr []int, i, j int) []int { temp := arr[j] arr[j] = arr[i] arr[i] = temp return arr}
algorithmic descriptions
The direct selection sorting of n records can get ordered result through n-1 direct selection sorting. The algorithm is described as follows:
Place an imaginary wall on the far left of the numeric list, with sorted sublists on the left and unsorted sublists on the right.
Find (select) the smallest (or largest) element in the unsorted sublist.
Swaps the selected element with the first element in the unsorted list.
Move the imaginary wall one position to the right.
Repeat steps 2 to 4 until the entire list of numbers is sorted (n - 1 rounds required).
code demonstrates
func selectSort(arr []int) []int { if len(arr) arr[j+1] { Swap(arr, j, j+1) } } } return arr}func Swap(arr []int, i, j int) []int { temp := arr[j] arr[j] = arr[i] arr[i] = temp return arr}
algorithmic descriptions
The basic idea of quick sorting: the records to be sorted are separated into two independent parts by one sorting, in which the keywords of one part of the records are smaller than those of the other part, and the two parts of the records can be sorted separately to achieve the order of the whole sequence.
One element is selected from the sequence and is called a "pivot."
Reorder the sequence so that all elements smaller than the reference are placed in front of the reference and all elements larger than the reference are placed behind the reference (the same number can be placed on either side). After this partition exits, the datum is in the middle of the series. This is called a partition operation.
recursively sort subarrays of elements smaller than the reference value and subarrays of elements larger than the reference value.
code implementation
//quickSort func quickSort(arr []int) []int { if len(arr) middle { right = append(right, arr[i]) } else { left = append(left, arr[i]) } } middle_s := []int{middle} left = quickSort(left) right = quickSort(right) arr = append(append(left, middle_s...), right...) return arr} The content of "How Golang implements common sorting algorithms" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.