插入、冒泡、快排、归并排序算法的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)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值