In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use python to achieve selection sorting, the article is very detailed, has a certain reference value, interested friends must read it!
Choose sorting for simple understanding: in ascending order, each time you traverse the array to be sorted, the default first element is the smallest, and then compare the remaining elements with the first element one by one, and if it is less than that, the two exchange positions, so that after traversing the array once, you can be sure to put the smallest element in the first place. Each time the array is traversed, a relative minimum element can be determined, so compared with the last traversed array, the array length minus 1 means that the smallest element has been determined to no longer participate in the subsequent traversal. Normally, the sorting can be completed by minus 1 traversal of the original array length.
Def selection_sort (alist):''selection sort' 'last = len (alist) first = 1 while first < last: print (first) # can be understood as how many times to traverse for i in range (first,last): if alist [I] < alist [first-1]: # arrange alist from small to large [first-1], alist [I] = alist [I] Alist [first-1] print (alist) # the result of each traversal first + = 1
Return alist
If _ _ name__ = ='_ _ main__': alist = [2, 4, 7, 1, 6, 5, 3] print (selection_sort (alist))
Code flow: first get the array length last. During the initial traversal, the element at the default 0 index position is the smallest, so making first 1 first means the first index of the element compared with the default minimum value (for example, in the first round of traversal, the element with index 1 must be compared with the element with index 0, and the traversal of each round later can be inferred similarly) Then enter the while loop, where the condition is judged by the index of the element and the length of the array at the beginning of the comparison. If it is less than that, it will be traversed, otherwise the loop will be terminated (this content will be explained in detail for the example results below) For the internal for loop, pay attention to its scope: the latter is less than the former exchange position, otherwise it will not be exchanged. Here we can see that the index of the latter element minus 1 is the index of the former element, or take the first traversal as an example. When first is 1, the minus 1 is 0, so the comparison between the first element and the element with the default minimum 0 position is realized. On the second traversal, first is 2, minus 1 is 1. The comparison between position 2 and position 1 can be achieved (at this time 1 is the index'0' of the first element of the array for the second traversal). No, no, no. no, no, no. Sort by analogy, and after each traversal, you can view the results of each time by printing the array. First plus 1 is because each iteration subtracts the length of the array to be sorted by 1. Let's talk about it in combination with the example results:
1 # first value [1,4,7,2,6,5,3] # first traversal result 2 # first value [1,2,7,4,6,5,3] # second traversal result 3 # first value [1,2,3,7,6,5,4] # third traversal result 4 # first value [1,2,3,4,7,7,6,6,5] # fourth traversal result 5 # first value [1,2,3] 4, 5, 7, 6] # Fifth traversal result 6 # first value [1, 2, 3, 4, 5, 6, 7] # sixth traversal result
[1,2,3,4,5,6,7] # final result
Result analysis: if the length of the array to be sorted is 7, it only needs to be traversed 6 times, and if 6 elements are determined in the first 6 times, there is no need to compare the last element. When first is 6, 6
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.