《算法导论》第2章算法基础——代码实现(Python)

# 插入排序 (非降序)

A = [5, 2, 4, 6, 1, 3]
j = 1
length = len(A)
for j in range(1, length):
    key = A[j]
    i = j - 1
    while i > -1 and A[i] > key:
        A[i+1] = A[i]
        i = i - 1
    A[i+1] = key
print(A)

# 插入排序 (非升序)

A = [5, 2, 4, 6, 1, 3]
j = 1
length = len(A)
for j in range(1, length):
    key = A[j]
    i = j - 1
    while i > -1 and A[i] < key:  # 只要改变这里即可
        A[i+1] = A[i]
        i = i - 1
    A[i+1] = key
print(A)

# 线性查找

A = [5, 2, 4, 6, 1, 3]
is_exist = False
v = int(input("请输入v:"))
for i in A:
    if i == v:
        is_exist = True
        break
if is_exist:
    print("Get")
else:
    print("NIL")

# n位二进制数相加,输出结果

x = [int(i) for i in "11011001"]
y = [int(i) for i in "01101101"]
result = []
length = len(x)
carry = 0
for i in range(length - 1, -1, -1):
    temp = x[i] + y[i]
    if temp + carry == 2:
        carry = 1
        r = 0
    elif temp + carry == 3:
        carry = 1
        r = 1
    else:
        r = temp + carry
        carry = 0
    result.insert(0, r)
result.insert(0, carry)
print(result)

# 选择排序

A = [5, 2, 4, 6, 1, 3]
length = len(A)
for i in range(length-1):  # 只要到n-1即可,依次选择最小交换,那么最后剩下的一定是最大的
    min_key = A[i]
    for j in range(i+1, length):
        if A[j] < min_key:
            A[i] = A[j]
            A[j] = min_key
            min_key = A[i]
print(A)

# 归并排序(有哨兵)

def merge(A, p, q, r):  # 合并函数, A为数组, A[p...q], A[q+1...r]  注:r是最后一个元素的下标
    n1 = q - p + 1
    n2 = r - q
    L = []  # 左数组
    R = []  # 右数组
    for i in range(n1):
        L.append(A[p + i])  # 从A[p]开始到A[q]复制给L数组
    for i in range(n2):
        R.append(A[q + i + 1])  # 从A[q+1]开始到A[r]复制给R数组
    L.append(float("inf"))
    R.append(float("inf"))
    i = 0
    j = 0
    for k in range(p, r + 1):
        if L[i] <= R[j]:
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            j = j + 1


def merge_sort(A, p, r):
    if p < r:
        q = (p + r) // 2
        merge_sort(A, p, q)
        merge_sort(A, q+1, r)
        merge(A, p, q, r)


A = [5, 2, 4, 6, 1, 3]
merge_sort(A, 0, len(A) - 1)  # 传最后一个元素的下标
print(A)

# 归并排序 (无哨兵版)

def merge(A, p, q, r):  # 合并函数, A为数组, A[p...q], A[q+1...r]  注:r是最后一个元素的下标
    n1 = q - p + 1
    n2 = r - q
    L = []  # 左数组
    R = []  # 右数组
    for i in range(n1):
        L.append(A[p + i])  # 从A[p]开始到A[q]复制给L数组
    for i in range(n2):
        R.append(A[q + i + 1])  # 从A[q+1]开始到A[r]复制给R数组
    i = 0
    j = 0
    for k in range(p, r + 1):
        #  若数组越界,将剩余元素赋给A中
        if i >= n1:  # L越界
            for t in range(j, n2):  # 将R数组剩余部分复制给A
                A[k] = R[t]
                k += 1
            break
        elif j >= n2:  # R越界
            for t in range(i, n1):  # 将R数组剩余部分复制给A
                A[k] = L[t]
                k += 1
            break
        else:
            if L[i] <= R[j]:
                A[k] = L[i]
                i = i + 1
            else:
                A[k] = R[j]
                j = j + 1


def merge_sort(A, p, r):
    if p < r:
        q = (p + r) // 2
        merge_sort(A, p, q)
        merge_sort(A, q+1, r)
        merge(A, p, q, r)


A = [5, 2, 4, 6, 3, 1, 7, 0]
merge_sort(A, 0, len(A) - 1)  # 传最后一个元素的下标
print(A)


# 二分查找

A = [1, 2, 3, 7, 10, 15, 17]
v = int(input("输入你想查找的数: "))
length = len(A)
left = 0
right = length - 1
mid = (left + right) // 2
for i in range(length):
    if left > right:
        print("未找到")
        break
    if v == A[mid]:
        print("找到")
        break
    elif v > A[mid]:
        left = mid + 1
        mid = (left + right) // 2
    else:
        right = mid - 1
        mid = (left + left) // 2

# 插入排序二分法

def binary_search(A, left, right, v):
    while left <= right:
        mid = (left + right) // 2
        if left == right:
            if key > A[left]:
                return left + 1
            else:
                return left
        if v < A[mid]:
            right = mid - 1
        elif v > A[mid]:
            left = mid + 1
        else:
            return mid
    return mid


A = [5, 2, 4, 6, 3, 1, 7, 0]
length = len(A)
for j in range(1, length):
    if A[j-1] > A[j]:
        key = A[j]
        loc = binary_search(A[:j], 0, j - 1, key)
        for t in range(j, loc, -1):
            A[t] = A[t-1]
        A[loc] = key
print(A)

你可能感兴趣的:(算法导论,算法,python)