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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve quick sorting in Python, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Quick sort (Quick Sort)
The idea of merging and sorting: split and merge, and complete the sorting in the process of merging.
The idea of quick sorting: sorting and splitting
According to a "median" data item, the data table is divided into two halves, less than half of the median and more than half of the median, and then each part is quickly sorted recursively.
If you want the number of data items in the left and right parts to be the same, you should find the median of the data table, but finding the median requires a lot of computational overhead, usually choosing the first element as the median
Quick sort code ideas:
Set the first element as the median, starting with the second element, and divide it into two parts
Put the first element (median) in the right place
Three elements of recursion:
Basic end condition: if there is only one element in the data table, it means it has been sorted.
Downsize: divide the datasheet into two parts based on the median
Call yourself: the two parts call themselves for quick sorting
The method of dividing a data table into two parts:
Set the left and right labels (left and right), and take the first element as the median
The left starts to move to the right from the second element and stops when it is larger than the median.
The right mark (right) starts to move to the left from the end, and stops if it is smaller than the median.
Judge whether the left mark has exceeded the right mark.
If not, exchange the data referred to by the left and right marks
If left > right, the movement stops, and the right position is the median position. Swap the median value to right.
The process of completing the split actually completes part of the sorting work. Since the while loop will not continue until the end of execution (that is, left and right will stop), it is necessary to determine whether it has been left > right.
Algorithm analysis
The quick sort process is divided into two parts: split and move
Splitting process: if the two parts are of the same size, the time complexity is O (logn).
Mobile process: time complexity O (n)
To sum up, the time complexity of quick sorting is O (nlogn)
And no additional storage space is needed during the quick sort run.
However, if we are unlucky, the split point of the median is too far from the middle, resulting in an imbalance between the left and right parts. in extreme cases, some of them always have no data, so the time complexity is reduced to O (n ^ 2).
After reading the above, do you have any further understanding of how to implement quick sorting in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.