In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what sort algorithms are commonly used in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
A brief introduction of commonly used sorting algorithms
The following mainly starts from the basic concepts and principles of the sorting algorithm, and analyzes and compares the time complexity, space complexity, stability and speed of the algorithm. According to the size of the problem to be sorted (the number of records n), the memory space needed in the sorting process is also different, so the sorting algorithm is divided into two categories: [inner sorting] and [external sorting].
Internal sorting: when sorting, all the data elements are stored in the computer's random access memory RAM.
External sorting: a sorting process in which the number of records to be sorted is so large that memory cannot hold all records at one time, and external memory needs to be accessed in the sorting process.
First take a look at the classification relationship of common sorting algorithms (see figure 1-1)
Figure 1-1 Common sorting algorithms
2. Internal sorting correlation algorithm
2.1 insert sort
Core idea: insert a data element to be sorted into the appropriate position in the previously sorted series, so that the data elements are still in order until all the data elements to be sorted have been inserted.
2.1.1 directly insert sort
Core idea: to insert the key of the I data element with the previously sorted iMel 1, iMel 2, iMel 3, … The values of the data elements are compared in order, and the insertion position of the first data element is found by this linear search method, and the order of the data elements in the original position is moved back until all the data elements are in order.
In the direct insertion sorting, the data elements with the same keywords will keep the original position unchanged, so the algorithm is stable, the worst value of time complexity is O (N2), and the space complexity is constant O (l).
Python source code:
#-insert sort directly-- def insert_sort (data_list): # iterate through all elements in the array, where the index element 0 is sorted by default So starting with 1, for x in range (1, len (data_list)): # compares the element to the sorted preordered array in turn, if the element is small Then exchange # range (XMub 1): reverse cycle from Xmuri 1 to 0 for i in range (XMub 1): # judgment: exchange if data_ list [I] > data_list [item1]: temp = data_ list [I + 1] data_ list [I + 1] = data_ list [I] data_ list [I] = temp
2.1.2 Hill sort
The core idea is to group the records according to a certain increment of the subscript and sort each group using the direct insertion sorting algorithm; as the increment decreases gradually, each group contains more and more keywords, and when the increment is reduced to 1, the whole file is divided into a group, and the algorithm is terminated.
Hill sort time complexity will be better than O (N2), however, in multiple insert sort, * insert sort is stable, but in different insert sort process, the same element may move in their respective insert sort, so Hill sort is unstable.
Python source code:
#-Hill sort-- def insert_shell (data_list): # initialization step value Here, half the length of the sequence is used to assign it a value group = int (len (data_list) / 2) # * layer loop: change the group value to group the list in turn. While group > 0: # the following: sort the grouped data using the idea of direct insertion sorting # range (group,len (data_list)): start with group (group,len (data_list)): # range -group): start from x-group to start reverse order comparison with selected elements Interval between each comparison element group for j in range (i-group,-1,-group): # if two elements in the group satisfy the exchange condition, exchange if data_ [j] > data_list [j+group]: temp = data_ list [j + group] data_ list [j + group] = data_ list [j] data_ list [j] = temp # while loop condition half group = int (group / 2)
2.2 Select sort
Core idea: during each scan, select the element with the smallest key or * * from the data elements to be sorted, and put them in the * of the ordered sequence until all the data elements to be sorted have been sorted.
2.2.1 Direct selection sort
Core idea: select the data element with the least key for each location, that is, select the smallest element to exchange with the element in * position, and then select the smallest element to exchange with the element in the second position among the remaining elements. until the penultimate element is compared with an element.
According to its basic idea, every time a scan takes place, if the current element is smaller than an element, and this small element appears behind an element equal to the current element, then their positions are exchanged, so when sorting is unstable, the time complexity is O (N2) and the space complexity is O (l).
Python source code:
#-Direct selection sort-- def select_sort (data_list): # traverses each element in the sequence for i in range (0) Len (data_list): # define the minimum value in this cycle minimum = data_ list [I] # compare this element with the remaining elements in turn to find the smallest element for j in range (item1, len (data_list)): if data_ list [j]
< minimum: temp = data_list[j]; data_list[j] = minimum; minimum = temp #将比较后得到的真正的最小值赋值给当前位置 data_list[i] = minimum 2.2.2 堆排序 堆排序时对直接选择排序的一种有效改进。 核心思想:将所有的数据建成一个堆,***的数据在堆顶,然后将堆顶的数据元素和序列的***一个元素交换;接着重建堆、交换数据,依次下去,从而实现对所有的数据元素的排序。完成堆排序需要执行两个动作:建堆和堆的调整,如此反复进行。 堆排序有可能会使得两个相同值的元素位置发生互换,所以是不稳定的,其平均时间复杂度为0(nlog2n),空间复杂度为O(l)。 Python源代码: #-------------------------堆排序-------------------------------- #**********获取左右叶子节点********** def LEFT(i): return 2*i + 1 def RIGHT(i): return 2*i + 2 #********** 调整大顶堆 ********** #data_list:待调整序列 length: 序列长度 i:需要调整的结点 def adjust_max_heap(data_list,length,i): #定义一个int值保存当前序列***值的下标 largest = i #执行循环操作:两个任务:1 寻找***值的下标;2.***值与父节点交换 while 1: #获得序列左右叶子节点的下标 left, right = LEFT(i), RIGHT(i) #当左叶子节点的下标小于序列长度 并且 左叶子节点的值大于父节点时,将左叶子节点的下标赋值给largest if (left < length) and (data_list[left] >Data_ list [I]: largest = left # print ('left leaf node') else: largest = I # when the subscript of the right leaf node is less than the sequence length and the value of the right leaf node is greater than the parent node, assign the subscript value of the right leaf node to largest if (right
< length) and (data_list[right] >Data_ list: largest = right # print ('right leaf node') # if largest is not equal to I, the current parent node is not a * * value Need to exchange the value if (largest! = I): temp = data_ list [I] data_ list [I] = data_ list [highest] data_ [list] = temp I = largest # print (largest) continue else: break # * build a large top stack * def build_max_heap (data_list): length = len (data_list) for x in range (int ((length-1) / 2),-1 -1): adjust_max_heap (data_list,length,x) # * heap sort * def heap_sort (data_list): # create a large top heap first Ensure that the * value is at the root node And the value of the parent node is greater than the leaf node build_max_heap (data_list) # I: the length of the sequence in the current heap. Initialize to the length of the sequence I = len (data_list) # execute loop: 1. Each time the top element of the heap is taken out and placed in the sequence of * (len-1,len-2,len-3...) # 2. Adjust the heap so that it continues to satisfy the properties of the large top heap. Notice that the length of the sequence in the heap is modified in real time while I > 0: temp = data_ list [I-1] data_ list [I-1] = data_list [0] data_list [0] = temp # sequence length in the heap minus 1 I = iMet 1 # adjust the large top heap adjust_max_heap (data_list, I, 0)
2.3 Exchange sort
Core idea: as the name implies, it is a group of data elements to be sorted, in which the keys are compared with each other in the order of position, and if it is in reverse order, the two data elements are exchanged until the sequence data elements are ordered.
2.3.1 Bubble sort
Core idea: for a group of data elements to be sorted, regard each data element as a heavy bubble, according to the principle that light bubbles cannot be under heavy bubbles, compare and adjust the two adjacent elements from top to bottom, so that the heavier elements sink and the lighter ones rise.
According to the basic idea, the position of two elements will be changed only when the order of the two elements is opposite to the sorting requirements, otherwise it will remain the same, so the bubbling sorting is stable. The time complexity is O (N2) and the space complexity is O (l).
Python source code:
#-Bubble sort-- def bubble_sort (data_list): length = len (data_list) # the sequence length is length, and you need to perform length-1 round exchange for x in range (1 Magi length): # for each exchange round Compare the left and right elements in the sequence # in each exchange, because the elements of the sequence * * must be *, each cycle to the unsorted position of the sequence can be for i in range (if data_ list [I] > data_list [I]: temp = data_ list [I] data_ list [I] = data_ list [I + 1] data_ list [I + 1] = temp
2.3.2 Quick sort
Quick sorting is an essential improvement on bubble sorting.
Core idea: it is a local sorting, divide and conquer, large-scale recursive algorithm. That is, after a scan, make sure that the left element of the datum point is smaller and the right element is larger, and then recursively process the left and right elements until there is only one element on the left and right side of the datum point.
Fast sorting is an unstable algorithm, the time complexity of the worst value is O (N2), and the space complexity is O (log2n).
Python source code:
#-Quick sort-- # data_list: the sequence to be sorted; index # at the end of the index,end sequence at the beginning of start sorting: for sequences of length length: start = 0 domestic end = length-1 def quick_sort (data_list,start,end): if start
< end: i , j , pivot = start, end, data_list[start] while i < j: #从右开始向左寻找***个小于pivot的值 while (i < j) and (data_list[j] >= pivot): J = if 1 # move values less than pivot to the left if (I < j): data_ list [I] = data_ list [j] I = iList1 # look from left to right for * values greater than pivot while (I < j) and (data_ list [I] < pivot): I = iList1 # move values greater than pivot to the right if (I < j): data_list [j] = data_ List1 ] j = jmur1 # after the loop ends The values on the left are all less than pivot, and the values on the right are all greater than pivot # pivot. The position moves correctly. Then you only need to call this function for further sorting on the left and right sides of the sequence # Recursively call the function: for the left sequence: from 0 to end data_ 1 [I] = pivot # the left sequence continues to sort quick_sort (data_list,start,i-1) # the right sequence continues to sort quick_sort (data_list,i+1,end)
2.4 merge sort
The core idea is to recursively divide the data sequence into short sequences, that is, divide 1 into 2, 2 into 4, decompose them in turn, sort these groups when there is only one group, and then merge back to the original sequence in turn. Merge until the original sequence is all in order.
In the process of merging, we can ensure that the two equal current elements are saved in front of the result sequence, so the merge sorting is stable, its time complexity is O (nlog2n), and the space complexity is O (n).
Python source code:
#-merge sort-- # this is the merged function # merge the sequence data_ list [first.mid] with the sequence data_ list [mid + 1...last] def mergearray (data_list,first,mid,last,temp): # pair iPhore j K assigns the values respectively, I _ first,mid+1,0 _ j _ j _ k = compare when there are numbers on both the left and right sides. Take the smaller number while (I)
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.