Python classic sort method

Sorting data is a step in solving practical problems often use one of the test sites is the data structure, here are 10 kinds of classical sorting method.

First, a sorting method can be roughly divided into insertion sort, selection sort, exchange sort, merge sort, and bucket sort four categories, which are divided into direct insertion sort insertion sort, insertion sort and Hill binary sorting, selection sort divided into direct selection sorting and heap sorting, exchange sorting and into bubble sort quick sort, to sort the bucket sorting and counting radix sort represented. These time ordering method complexity and space complexity are shown in the table below.

Stability of ordering method is defined as: If there is a [i] and a [j], a [i] = a [j] && i <j in to be sorted sequence, if the sorted still conform to a [i] = a [j] && i <j, i.e., the relative positional relationship thereof does not change before and after the sorting algorithm is stable.

(1) direct insertion sort

The basic idea is to sort insert data into the appropriate position. Sequence shown below [6,8,1,4,3,9,5,0], in ascending order as an example.

a.6 <8, in line with ascending

b. 8> 1, does not meet the requirements, a change of position. First Comparison 1 and less 8,1, 1 and 8 exchange position, the sequence becomes [6,1,8,4,3,9,5,0], and then the comparison continues 6,1 1 smaller, exchange 1 and 6 positions, the sequence becomes [1,6,8,4,3,9,5,0], attention before this time has three values ​​meet the requirements in ascending order.

c. 8> 4, does not meet the requirements, changes in position 4, in accordance with the above method, followed by comparison with the previous value, respectively 6 and 8 exchange position, when comparing to the 1, 1 <4, do not change position. In this case the sequence becomes [1,4,6,8,3,9,5,0]

d. Repeat the full operation, until the entire sequence of the iteration completes.
Here Insert Picture Description

Thus, in fact, it is to ensure insertion sort traversed sequences are ordered, then the next access to the value, and by comparing the exchange position, inserted into the ordered sequence, when after all values ​​have been visited, the entire the sequence is ordered. Therefore, this method is stable, the spatial complexity is O (1), the best time complexity is O (n), the following code

def insert_sort(arr):
    for i in range(len(arr)-1):
        if arr[i+1]<arr[i]:#如果arr[i+1]较小,将其插入到前面升序序列中
            for j in range(i+1,0,-1):
                if arr[j]<arr[j-1]:#依次将大于arr[i+1]的值向后移动,直到找到不大于arr[i+1]的值
                    arr[j-1],arr[j]=arr[j],arr[j-1]
                else:
                    break
    return arr
在学习过程中有什么不懂得可以加我的
python学习扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容

(2) binary insertion sort

Thought binary insertion sort and direct insertion sort the same, only the data into the ordered sequence when using a binary lookup thought, i.e. the value before the intermediate position compared to shorten the time to find. code show as below

def BinaryInsert_sort(arr):
    for i in range(1,len(arr)):
        if arr[i]<arr[i-1]:
            left,right=0,i-1
            while left<right:#最终right位置的值是有序序列中第一个不大于arr[i]的值
                mid=left+(right-left)//2
                if arr[i]<arr[mid]:#
                    right=mid
                else:
                    left=mid+1
            for j in range(i,right,-1):
                arr[j],arr[j-1]=arr[j-1],arr[j]
    return arr

(3) Shell sort

Hill sorting based on direct insertion sort, is a sequence length n, a first take an integer less than n d1, d1 is the distance for all sequences of a set of data, the following drawings, in increments of 2 i, 1 , 4 and 5 as a group, a group of 8,3,0, 1,9 1 group, sort and then directly inserted within the group, and then rounded d2, d2 <d1, repeat the above steps until the increment reduced to one. Usually half the initial sequence is taken increments each subsequent half. Thus by the number of increments apart, it enables the number of elements across the plurality of moving speed.

code show as below

def Hill_sort(arr):
    d=len(arr)//2
    while(d>=1):
        for i in range(len(arr)//d):
            for j in range(i,len(arr)-d,d):
                if arr[j+d]<arr[j]:
                    for k in range(j+d,i,-d):
                        if arr[k]<arr[k-d]:
                            arr[k],arr[k-d]=arr[k-d],arr[k]
        d=d//2
    return arr

Because Hill sorting cross from access to the elements, and therefore unstable. Space complexity is O (1), the best time complexity is O (n).

(4) Direct Selection Sort

Thought is selected sort each selection maximum or minimum value from the random sequence, and the value of the first switching position. The sequence L, if it is in ascending order, for the first time and find the minimum value of l l [0] exchange, the second to identify l [1:] minimum value, and l [2] exchange, and so on, code show as below.

def select_sort(arr):
    for i in range(len(arr)-1):
        minnum=arr[i]
        m=i
        for j in range(i+1,len(arr)):
            if arr[j]<minnum:
                minnum=arr[j]
                m=j
        arr[i],arr[m]=arr[m],arr[i]
    return arr

Direct selection sort is stable, spatial complexity is O, constant time complexity of O (n2), because the original sequence is ordered step does not affect the selection sort (1).

(5) heapsort

Heap sort uses the concept of heap big top and small top of the heap. Big top stack is the largest binary parent node, i.e. ARR [I]> = ARR [2 I +. 1] && ARR [I]> = ARR [2 I-. 1]; minor vertex stack is the smallest binary value of the parent node, i.e. ARR [I] <= ARR [2 I +. 1] && ARR [I] <= ARR [2 I-. 1].

In ascending order as an example, first establish initial maximum stack entry to a node, its children nodes adjust downward, so that in line with the characteristics of a large stack top. The process of establishing an initial heap big top adjustment will repeat some nodes, because the switching nodes of the parent node and child, the child will be affected in the subtree root node, as shown in [9,1,7] meet the requirements of the original subtree after 6 and 9 exchange, [6,1,7] does not meet the requirements, it is necessary to adjust again the subtree.

After the initial establishment of large top stack, the displacement value to the root end of the sequence, and then readjust the foregoing sequence, then only to adjust downward from the root node as an initial large top stack has been formed, as shown, even if the exchange position 0 and 4, 8 parent also remains the largest.

def heap_sort(arr):
    def adjust(arr,node,maxid):
        root=node
        while True:
            child=2*root+1
            if child>maxid:
                return 
            child=child+1 if arr[child+1]>arr[child] and child+1<maxid else child#child是两个孩子中值较大的一个
            if arr[root]<arr[child]:
                arr[root],arr[child]=arr[child],arr[root]
                root=child
            else:
                return 
    first=len(arr)//2-1
    for node in range(first,-1,-1):
        adjust(arr,node,len(arr)-1)
    for maxid in range(len(arr)-1,0,-1):
        arr[0],arr[maxid]=arr[maxid],arr[0]
        adjust(arr,0,maxid-1)

Heap sort unstable space complexity is O (1), but the constant time complexity O (nlogn).

(6) bubble sort

A Sorting i.e. by exchanging element positions, so that an orderly sequence. Bubble sort is the sort simple exchange, each time the maximum value / minimum value are sequentially switched to the end of the sequence. Bubble sort is stable, spatial complexity is O (1), preferably the time complexity is O (n).

def bubble_sort(arr):
    for i in range(len(arr)-1):    # 这个循环负责设置冒泡排序进行的次数
        for j in range(len(arr)-i-1):  # j为列表下标
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

(7) quicksort

Quick sort can be said to be the most frequently used method for sorting and inspection frequency. The basic idea is based on a quick sort sequence is the standard, is greater than the value on the right side, is smaller than the value on the left, and then continue to repeat this step two values ​​of the sequence of the left and right sides, the idea is simple ,code show as below.

import random
def quick_sort(arr,start,end):
    if end-start<=0: return 
    index=random.randrange(start,end+1)
    print(index)
    arr[end],arr[index]=arr[index],arr[end]
    small=start-1
    for i in range(start,end):
        if arr[i]<arr[end]:
            small+=1
            if small!=i:#如果small与i不相同,说明有大于arr[end]的值出现,small记录的是分界线的位置
                arr[i],arr[small]=arr[small],arr[i]
    small+=1
    arr[end],arr[small]=arr[small],arr[end]
    print(arr)
    quick_sort(arr,start,small-1)
    quick_sort(arr,small+1,end)

For python, there is a more straightforward implementation

def qsort(arr):
    if len(arr) <= 1: return arr
    return qsort([lt for lt in arr[1:] if lt < arr[0]]) + arr[0:1]+ qsort([ge for ge in arr[1:] if ge >= arr[0]])    

Quick Sort does not create a new array, but the realization of the process utilizing the recursive function requires stack space, so the space complexity is O (nlogn), the best time complexity is O (nlogn).

(8) merge sort

Merge sort using a divide and conquer idea sequences into smaller sequences, after sequence sorting small, small large sequences into a sequence, the procedure as shown below.
Here Insert Picture Description

code show as below

def merge_sort(arr,start,end):
    def merging(l1,l2,lnew=[]):
        i,j=0,0
        while(i<len(l1) and j<len(l2)):
            if l1[i]<l2[j]:
                lnew.append(l1[i])
                i+=1
            else: 
                lnew.append(l2[j])
                j+=1
        if i<len(l1):
            for num in l1[i:]:
                lnew.append(num)
        if j<len(l2):
            for num in l2[j:]:
                lnew.append(num)
        return lnew
    if end-start>1:#分
        mid=start+(end-start)//2
        merge_sort(arr,start,mid)
        merge_sort(arr,mid,end)
        arr[start:end]=merging(arr[start:mid],arr[mid:end],[])#治
    return arr

Merge sort sequence need for new storage array combined space complexity is O (n), a constant time complexity is O (nlogn), it is stable.

(9) counting sequencing

Bucket sort is actually a general term for a class sorting methods. Bucket sort of thinking is all the data according to certain mapping relationship into a number of buckets, each bucket will sort the data, then combine all the barrels, so different mapping relationships may produce different sort of bucket and bucket sort is based on other sorting algorithms, data sorting bins need to use other sorting algorithms. Sorting and counting radix sort are the two representatives of the bucket sort.

Counting sequencing using the mapping relationship is the most direct f [i] = i, i.e., a bucket number is the same, there is no step of sorting bins. Specific process,

1. Locate the array to be sorted maximum and minimum elements
2. The number of statistics for each element in the array is i occurring, item stored in the array C i, then the mapping process
3. All counts accumulation, C [i] that is less than the number of the meaning element i in the array, that is sorted should be put in the position i
4. reverse filled destination array: i on each element of the new array C ( item i), each element will be put C (i) minus one.

code show as below

在学习过程中有什么不懂得可以加我的
python学习扣扣qun,784758214
群里有不错的学习视频教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容
def count_sort(arr):
    crr=[]
    m=max(arr)+1
    ll=len(arr)
    ans=[]
    for i in range(ll):#用于存放排序后的数组
        ans.append(None)
    for i in range(m):#桶
        crr.append(0)
    for num in arr:#映射的过程,计数
        crr[num]+=1
    for i in range(1,m):#计数累加
        crr[i]=crr[i-1]+crr[i]
    for num in arr:#反向填充数组
        ans[crr[num]-1]=num
        crr[num]-=1
    return ans

(10) radix sorting

Radix sorting method of mapping digital data in accordance with a particular bit into different buckets in accordance with the array sequence of bits into different buckets, and then sequentially removed in the order of bit 0 to 9, and then follow ten into different buckets, ...... until the highest bit data of the last time the data out, sorting is completed, for example:

Implementation code

def radix_sort(arr):
    def compare(basis,arr):#以数组basis为依据的插入排序
        for i in range(1,len(basis)):
            if basis[i]<basis[i-1]:
                for j in range(i,0,-1):
                    if basis[j-1]>basis[j]:
                        basis[j],basis[j-1]=basis[j-1],basis[j]
                        arr[j],arr[j-1]=arr[j-1],arr[j]
                    else:
                        break
    upnum=max(arr)
    tmp=1
    while(tmp<upnum):
        basis=[num//tmp%10 for num in arr]
        compare(basis,arr)
        tmp*=10
    return arr

Note radix sort in the bucket sort, be sure to use a stable sort method to sort the results before so that it can be used, otherwise the previous sort is meaningless.

Published 62 original articles · won praise 3 · Views 1376

Guess you like

Origin blog.csdn.net/NNNJ9355/article/details/103916431