In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to achieve bubble sorting in Python, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
What is the bubble sort?
Bubble sorting (Bubble Sort) is also a simple and intuitive sorting algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order.
The work of visiting the sequence is repeated until there is no need for swapping, that is, the sequence has been sorted. The algorithm gets its name because smaller elements slowly "float" to the top of the sequence by swapping.
As one of the simplest sorting algorithms, Bubble sorting makes me feel like Abandon in a word book, always at the top of the first page, so I'm most familiar with it.
Another optimization algorithm for bubble sorting is to set up a flag. When elements are not exchanged in a sequence traversal, it is proved that the sequence has been ordered. But this optimization doesn't do much to improve performance.
Algorithm step
Step1: compare adjacent elements. If the first is bigger than the second, swap places for both of them.
Step2: do the same for each pair of adjacent elements, from the first pair to the last pair. After this step is done, the maximum number is the last element.
Step3: repeat the above steps for all elements except the last one (because it is already sorted).
Step4: continue to repeat the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.
Code implementation
Import timedef waste_time (func): # decorator def function (* args,**kwargs): start_time=time.time () result=func (* args,**kwargs) end_time=time.time () spend=end_time-start_time print ("function% s takes% .3f seconds:"% (func.__name__,spend)) return result return function
@ waste_timedef bubbleSort (alist): # pass in a list. If it is ordered, you only need to test one round to see if the exchange is made, if not. If it is an ordered list, exit the loop n=len (alist) for i in range (NMUI 1): count = 0 # to mark whether there is an exchange (for j in range): if (alist [j] > alist [junk 1]): alist [j], alist [Jub1] = Alist [j + 1] Alist [j] count+=1 # determines whether count is equal to 0 If it is 0, it means that if (count==0): break is not exchanged.
If _ _ name__ = ='_ _ main__': alist= [9, the init list is, 20, 6, 7, 30] # unordered list print ("the init list is:", alist) bubbleSort (alist) print ("the sorted list is:", alist) blist= [5,9, 20,30,6, 77,88] # ordered list bubbleSort (blist) print (blist)
After reading the above, have you mastered how to achieve bubble sorting in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.