In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to achieve quick sorting in Python". In daily operation, I believe many people have doubts about how to achieve quick sorting in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve quick sorting in Python". Next, please follow the editor to study!
With loops, the performance of the program may be better, but with recursion, the program is easier to understand.
For quick sorting, the way of thinking of the algorithm is from simple to difficult.
If it is a number, then return, if it is two numbers, direct comparison can quickly get the result, we hire a general thinking to consider, set a reference value, if greater than the reference value, then stored by the array on the right, if less than the reference value, it is stored on the left. In this way, three numbers and four numbers are all in this way of thinking. We can use recursion to deal with it.
The programs that can be executed are very short and are as follows:
Def quicksort (array):
If len (array) < 2:
Return array
Else:
Pivot = array [0]
Less = [i for i in array [1:] if i pivot]
Return quicksort (less) + [pivot] + quicksort (greater)
Print quicksort ([5, 11, 3, 5, 8, 2, 6, 7])
If you log the program, simply add a few print to see how each node performs:
Def quicksort (array):
If len (array) < 2:
Return array
Else:
Pivot = array [0]
Print ("pivot:", pivot)
Less = [i for i in array [1:] if i pivot]
Print ("greater", greater)
Print ("sum:", (less) + [pivot] + (greater))
Return quicksort (less) + [pivot] + quicksort (greater)
Print quicksort ([5, 11, 3, 5, 8, 2, 6, 7])
The generated log is as follows:
D:programspython2 .7python.exe C:/python/kmp/db_ops/quicksort.py
('pivot:', 5)
('less:', [3,5,2])
('greater', [11, 8, 6, 7])
('sum:', [3, 5, 2, 5, 11, 8, 6, 7])
('pivot:', 3)
('less:', [2])
('greater', [5])
('sum:', [2,3,5])
('pivot:', 11)
('less:', [8,6,7])
('greater', [])
('sum:', [8,6,7,11])
('pivot:', 8)
('less:', [6,7])
('greater', [])
('sum:', [6,7,8])
('pivot:', 6)
('less:', [])
('greater', [7])
('sum:', [6,7])
[2, 3, 5, 5, 6, 7, 8, 11]
Process finished with exit code 0
At this point, the study on "how to achieve quick sorting in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.