插入、冒泡、快排、歸併排序算法的python實現

直接插入、折半插入、希爾排序、冒泡、快速、歸併

 

#  1.插入排序____直接插入排序
#  時間複雜度O(n^2),空間複雜度O(n)
import numpy as np
def InsertSort(A):
    # A : np.array
    # n : number of A
    for i in range(1,len(A)):
        if A[i] < A[i-1]:
            temp = A[i]
            j = i-1
            while temp<A[j]:
                A[j+1] = A[j]
                j = j-1
            A[j+1] = temp
    return A
A = np.array([1,22,6,4,8])
B = InsertSort(A)
print(B)       
#  2.插入排序____折半插入排序(序列已經有序)
def InsertSort_1(A):
    for i in range(1,len(A)):
        temp = A[i]
        high = i-1
        low = 0
        while low<=high:
            mid = int((low + high)/2)
            if temp<A[mid]:
                high = mid-1
            else:
                low = mid+1
        j = i-1
        while j>=high+1:#j+1可手推
            A[j+1] = A[j]
            j = j-1
        A[high+1] = temp
    return A
A = np.array([1,7,6,10,12,5,4,3,2,8])
B = InsertSort_1(A)
print(B)

# 3. 插入排序____希爾排序
def ShellSort(A):
    dk = int(len(A)/2)
    while dk>=1:
        print(dk)
        for i in range(dk,len(A)):
            if A[i] < A[i-dk]:
                
                A[i], A[i-dk] = A[i-dk],A[i]
                '''
                temp = A[i]
                print(A[i])
                j = i - dk
                print(A[j])
                while temp<A[j]:
                    print('sss')
                    A[j+dk] = A[j]
                    j = j - dk
                A[j+dk] = temp
                
            else:
                #print('xxx')
                continue
                '''
        dk = int(dk/2)
        print(A)
    return A
A = np.array([1,7,6,10,12,5,4,3,2,8])
B = ShellSort(A)
print(B)    

# 4. 交換排序____冒泡排序,時間複雜度O(n^2),空間複雜度O(1)
def BubbleSort(A):
    num = 0
    for i in range(len(A)):
        for j in range(0,len(A)-i-1):
            num += 1
            if A[j+1]<A[j]:
                A[j+1],A[j] = A[j],A[j+1]
                print(A)
            else:
                continue
    print(num)
    return A
A = np.array([1,7,6,10,5,9,68,8,9,10,34])
B = BubbleSort(A)
print(B)    


# 5.  交換排序____快速排序
import numpy as np
def QuickSort(A, low, high):
    if low<high:
        position = Partition(A, low, high)
        QuickSort(A,low,position-1)
        QuickSort(A,position+1,high)
    return A

def Partition(A,low,high):
    i = low
    temp = A[low]
    while low<high:
        while low<high and A[high]>=temp:
            high -=1
        while low<high and A[low]<=temp:
            low +=1
        A[high],A[low] = A[low],A[high]#交換倆位置
    A[low],A[i] = temp,A[low]#交換temp與相遇i的位置 
    return low
#A = np.array([1,7,6,10,5,9,68,8,9,10,34])
A = np.random.randint(1,20,10)
print(A)
B = QuickSort(A,0,len(A)-1)
print(B) 


# 7,歸併排序,列表實現
def merge(a,b):
    c = []
    i = j =0
    while i<len(a) and j<len(b):
        if a[i]<=b[j]:
            c.append(a[i])
            i += 1
        else:
            c.append(b[j])
            j += 1
    while j<len(b):
        c.append(b[j])
        j += 1
    while i<len(a):
        c.append(a[i])
        i += 1
    return c

def mergeSort(mylist):
    if len(mylist) <= 1:
        return mylist
    mid = int(len(mylist)/2)
    left = mergeSort(mylist[:mid])
    right = mergeSort(mylist[mid:])
    return merge(left,right)

B = np.random.randint(1,7,10)
B.tolist()
print(B)
A = mergeSort(B)
print(A)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章