Common sorting algorithm implementation python

Common sorting algorithm implementation python

Refer to the following links: https://www.cnblogs.com/shiluoliming/p/6740585.html

Algorithm (Algorithm) refers to the exact problem-solving solutions and complete description, is a series of clear instructions to solve the problem, the algorithm represents a policy mechanism to solve the problem described in a systematic way. That is, a certain specification can be input to obtain the required output within a limited time. If an algorithm is defective, or not suitable for a problem, the implementation of this algorithm will not solve the problem. Different algorithms might use a different time, space or efficiency to accomplish the same task. The pros and cons of an algorithm can use the space complexity and time complexity of the measure.

An algorithm should have the following seven important features:

① finite resistance (Finiteness): refers to the finite algorithm must be able to terminate after a finite number of steps of the algorithm performed;

② certainty (Definiteness): each step of the algorithm must have a precise definition;

③ entry (Input): 0 algorithm or a plurality of inputs to characterize the initial condition of operation objects, a so-called 0 refers to input initial conditions fix algorithm itself;

④ output items (Output): an algorithm has one or more outputs, to reflect the results of processing the input data. No output of the algorithm is meaningless;

⑤ feasibility (Effectiveness): Any algorithm calculation step is performed can be substantially decomposed executable operation step, i.e., each calculation step can be done (also referred validity) for a limited time;

⑥ efficiency (High efficiency): execution speed, small footprint;

⑦ robustness (Robustness): correct data in response to

 

import random
#冒泡排序
def bubbleSort(L):
    assert (type(L)==type(['']))
    length=len(L)
    if length<=1:
        return L

    for i in range(length-1):
        for j in range(length-1-i):
            if L[j]<L[j+1]:
                L[j],L[j+1]=L[j+1],L[j]

    return L
#选择排序
def selectSort(L):
    assert (type(L)==type(['']))
    length=len(L)
    if length<=1:
        return L

    def _max(s):
        largest=s
        for i in range(s,length):
            if L[i]>L[largest]:
                largest=i
        return largest

    for i in range(length):
        largest=_max(i)
        if i!=largest:
            L[i],L[largest]=L[largest],L[i]

    return L

#插入排序
def insertSort(L):
    assert (type(L) == type(['']))
    length = len(L)
    if length <= 1:
        return L

    for i in range(1,length):
        value=L[i]
        j=i-1
        while j>=0 and L[j]<value:
            L[j+1]=L[j]
            j-=1
        L[j+1]=value
    return L
#归并排序法
def mergeSort(l):
    length = len(l)
    if length <= 1:
        return l

    num=int(length/2)
    left=mergeSort(l[:num])
    right=mergeSort(l[num:])
    return merge(left,right)

def merge(left,right):
    l,r=0,0
    result=[]
    while l<len(left) and r<len(right):
        if left[l]>right[r]:
            result.append(left[l])
            l+=1
        else:
            result.append(right[r])
            r+=1
    result+=left[l:]
    result+=right[r:]

    return result

#快速排序
def quickSort(l,s,e):
    if s<e:
        m=partition(l,s,e)
        quickSort(l,s,m-1)
        quickSort(l,m+1,e)
    return l

def partition(l,start,end):
    pivot=l[start]
    leftmark=start+1
    rightmark=end
    while True:
        while l[leftmark]<=pivot:
            if leftmark==rightmark:
                break
            leftmark+=1
        while l[rightmark]>pivot:
            rightmark-=1
        if leftmark<rightmark:
            l[leftmark],l[rightmark]=l[rightmark],l[leftmark]
        else:
            break
    l[start],l[rightmark]=l[rightmark],l[start]
    return rightmark

lst=[random.randint(1,10) for x in range(10) ]
print ('before sort:',lst)
print ('after quickSort:',quickSort(lst,0,len(lst)-1))
print ('after mergeSort:',mergeSort(lst))
print ('after insertSort:',insertSort(lst))
print ('after selectSort:',selectSort(lst))
print ('after bubbleSort:',bubbleSort(lst))

  

Guess you like

Origin www.cnblogs.com/marszhw/p/10962712.html