In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how python insertion sorting is optimized. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
When there is a large amount of data in the ordered interval, searching for the insertion location of the data can be very time-consuming.
The main results are as follows: 1. The insertion sorting algorithm always searches the insertion position from the ordered interval as the starting point.
2. The binary search method can be used to quickly confirm the position to be inserted, so there is an optimized version of the insertion sorting algorithm, also known as the binary search insertion algorithm.
Example
Def insert_sort2 (data_list):''use the binary search function to determine the insertion position of the element to be inserted in the ordered interval' count=0 # count the number of cycles length = len (data_list) for i in range (1 Magi length): # the element in the first position is the sorted interval by default So the subscript starts at 1, print (data_list) wait_insert_data = data_ insert_index,count1 [I] # # wait for the insertion element move_index = I insert_index,count1 = binary_search (data_ list [0: I] Wait_insert_data) # find the insertion position count+=count1 # count the number of cycles plus binary search while move_index > insert_index: # Mobile element Until count+=1 data_ list [move _ index] = data_ list [move _ index-1] move_index-= 1 data_ list [insert _ index] = wait_insert_data # insert operation print (data_list) print (f "total number of cycles is {count}") return data_list def binary_search (data_list) Data): "" input: ordered list And the data output of the data to be found: the data should be inserted in the ordered list where the count variable is used purely to count the number of cycles and can be removed in practical application. "" count = 0 length = len (data_list) low = 0 high = length-1 # # if the given element is greater than or equal to the last element, insert the last # # if it is less than the first element, insert position 0 if data > = data_list [length-1]: return length,0 elif data
< data_list [0]: return 0,0 insert_index = 0 while low < high-1: count +=1 mid = (low + high)//2 #python中的除法结果默认为浮点数取整数部分时使用 // if data_list[mid] >Data: high = mid insert_index = high else: low = mid insert_index = low+1 # if the value is the same or greater than the value of mid, then the insertion position is behind it, return insert_index,count. Thank you for reading! This is the end of this article on "how to optimize python insertion sorting". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.