# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.7.4 64-bit (''base'': conda)'
#     name: python37464bitbasecondab708f16640e64910af49310a539c91f5
# ---

# # 二分查找

def bin_search(li, val):
    low = 0
    high = len(li)
    if (high == 0):
        return -1
    while low <= high:
        mid = low + (high - low) // 2
        if li[mid] == val:
            return mid
        elif li[mid] < val:
            low = mid + 1
        else:
            high = mid - 1
    return -1


list1 = list(range(100))
bin_search(list1, 3)


# # 排序

# ## 冒泡排序
# 稳定排序

def bubble_sort(li):
    n = len(li)
    for i in range(n - 1):
        for j in range(0, n - i - 1):
            if(li[j] > li[j+1]):
                li[j], li[j+1] = li[j+1], li[j]


list1 = list(range(100))
li = list1[::-1]
bubble_sort(li)
print(li)


# ## 冒泡排序改进

def bubble_sort_update(li, val):
    n = len(li)
    for i in range(n - 1):
        flag = True
        for j in range(0, n - i - 1):
            if (li[j] > li[j + 1]):
                li[j], li[j + 1] = li[j + 1], li[j]
                flag = False
        if flag:
            break


# # 选择排序
# 不稳定

def select_sort(li):
    for i in range(len(li)):
        minIndex = i
        for j in range(i, len(li)):
            if li[j] < li[minIndex]:
                minIndex = j
        li[i], li[minIndex] = li[minIndex], li[i]


li = list(range(100))[::-1]
select_sort(li)
print(li)


# ## 插入排序

def insert_sort(li):
    n = len(li)
    for i in range(1, n):
        temp = li[i]
        j = i - 1
        while (j >= 0 and li[j] > temp):
            li[j + 1] = li[j]
            j = j - 1
        li[j + 1] = temp


li = list(range(100))[::-1]
insert_sort(li)
print(li)


# ## 快排
#
# 最坏情况：有序，由于递归可能会暴栈

# +
def partition(li, left, right):
    tmp = li[left]
    while left < right:
        while left < right and li[right] >= tmp:
            right -= 1
        li[left] = li[right]
        while left < right and li[left] < tmp:
            left += 1
        li[right] = li[left]
    li[left] = tmp
    return left

def quick_sort(li, left, right):
    if left < right:
        mid = partition(li, left, right)
        quick_sort(li, left, mid - 1)
        quick_sort(li, mid + 1, right)


# -

li = list(range(100))[::-1]
quick_sort(li, 0, len(li) - 1)
print(li)


# # 堆排序

# +
def sift(li, low, high):
    """
    # 堆调整
    # li表示树，low表示树的根，hight表示树的最后一个节点
    """
    tmp = li[low]
    i = low
    j = 2 * i + 1 # 左孩子节点
    while j < high:
        if j + 1 < high and li[j] < li[j + 1]:    # 如果右孩子存在且比左孩子大
            j += 1
        if li[j] > li[i]:
            li[i] = li[j]
            i = j
            j = 2 * i + 1
        else:   # 两个孩子都比根小
            break
    li[i] = tmp

def heap_sort(li):
    # 1. 构造堆
    n = len(li)
    for low in range(n//2 - 1, -1, -1):
        sift(li, low, n)
    # 2. 出数字
    for high in range(n - 1, -1, -1):
        li[0], li[high] = li[high], li[0]
        sift(li, 0, high - 1)


# -

list1 = list(range(100))[::-1]
heap_sort(list1)
print(list1)



StarcoderdataJupyterScriptsDedupFiltered