This article mainly introduces the principle of selecting sorting and Its Implementation in Python. the time complexity of selecting sorting is random (n & amp; sup2;). For more information, see
Basic Idea:Find the smallest element in the unsorted sequence, put it in the first place, and then find the smallest element in the unsorted sequence, put it in the second place, and so on, until all elements are sorted. Suppose there are n + 1 sequence elements in total, we need to find n rounds to sort the sequence. In each round, we can do this: Compare the first element of the unordered sequence with the subsequent element in sequence. If the subsequent element is small, then the next element and the first element are placed in the SWAp position. After such a round, the first element must be the smallest. In this way, n rounds can be sorted.
Schematic diagram
:
:
Initial data is not sensitive. no matter whether the initial data is sorted or not, it must undergo N2/2 comparisons, which is not advantageous for some originally sorted or approximately sorted sequences. In the best case, that is, all the good order, need 0 exchange, the worst case, reverse order, need N-1 exchange.
The number of data exchanges is small. If an element is in the correct final position, it will not be moved. In the worst case, only N-1 data exchange is required. In all the sorting methods that rely entirely on exchange to move elements, selecting sorting is better.
Python code implementation:
Def sort_choice (numbers, max_to_min = True, move x to the end of the column. [N1, n2, n3 ,... nn] 2. append x to the sorting result [n1, n3 ,... nn, n2] 3. n-1 elements after sorting [n1, n3 ,... nn], repeat the first step, repeat n-1 times. Max_to_min refers to the sorting from large to small. The default value is true; otherwise, the sorting is from small to large. For sorting [8, 4, 1, 0, 9], the general process is as follows: sorted_numbers = [] [8, 4, 1, 0, 9], sorted_numbers = [9] [4, 1, 0, 8], sorted_numbers = [9, 8] [1, 0, 4], sorted_numbers = [9, 8, 4] [0, 1], sorted_numbers = [9, 8, 4, 1] [0], sorted_numbers = [9, 8, 4, 1, 0] "if len (numbers) <= 1: return numbers sorted_list = [] index = 0 for I in xrange (len (numbers)-index ): left_numbers = _ numeric (numbers, max_to_min) numbers = left_numbers [:-1] numeric (left_numbers [-1]) index + = 1 return sorted_listdef _ numeric (numbers, get_max = True): ''' get the maximum or minimum value x, and extract x to put it at the end of the list. ex: get_max = True, [1, 4, 3] Values [1, 3, 4] get_max = False, [1, 4, 3] Values [4, 3, 1] ''' max_index = 0 for I, num in enumerate (numbers): if get_max: if num> numbers [max_index]: max_index = I else: if num <numbers [max_index]: max_index = I numbers = numbers [: max_index] + numbers [max_index + 1:] + [numbers [max_index] return numbers
Test:
>>> get_left_numbers([0, 4, 0, 31, 9, 19, 89,67], get_max=True)[0, 4, 0, 31, 9, 19, 67, 89]>>> get_left_numbers([0, 4, 0, 31, 9, 19, 89,67], get_max=False)[4, 0, 31, 9, 19, 89, 67, 0]>>> sort_choice([0, 4, 0, 31, 9, 19, 89,67], max_to_min=False)[0, 0, 4, 9, 19, 31, 67, 89]>>> sort_choice([0, 4, 0, 31, 9, 19, 89,67], max_to_min=True)[89, 67, 31, 19, 9, 4, 0, 0]