In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use go to implement the bubble sorting algorithm. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Basic ideas
By treating the sorting sequence from back to front, the sorting codes of the adjacent elements are compared in turn, and if the reverse order is found, the elements with smaller sorting codes are moved from the back to the front, just like underwater bubbles.
The popular point is that if the former element in the array is compared with the latter element, it will be exchanged if it is greater or less than the former, and the final sequence time complexity of the maximum or minimum returned to the array is O (n ^ 2).
The number of comparisons is:
As can be seen from the number of comparisons, it is a square level of time complexity
Bubble sorting algorithm is stable, if two adjacent elements are equal, will not be exchanged, can ensure the stability requirements.
2. Algorithm steps
For example, an array
{23,45,13,67,35}
Compare the previous number with the following number, and if the previous number is large, swap.
The first round begins to sort:
The first round of iteration begins, starting with the first number, comparing the two adjacent numbers in turn. If the previous number is larger than the next number, then swap positions until the last number is processed, and the last number is the maximum.
The second round begins to sort:
The second round begins to iterate, repeat the above steps, and get the second largest number from the bottom.
The third round begins to sort:
The third round begins to iterate, repeat the above operation, and get the third largest value from the bottom.
The fourth round begins to sort:
The fourth round begins to iterate, repeat the above operation, and get the fourth largest value from the bottom.
3. The algorithm implements package main import "fmt" func BubbleSort (arr [] int) [] int {for I: = 0; I
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.