【Python】排序算法和查找算法

排序算法(选择、冒泡和归并)

# 简单选择排序,从小到大排列
def select_sort(items, comp=lambda x, y: x < y):
    items = items[:]
    for i in range(len(items) - 1):
        min_index = i
        for j in range(i + 1, len(items)):
            if comp(items[j], items[min_index]):
                min_index = j
        items[i], items[min_index] = items[min_index], items[i]
    return items


if __name__ == '__main__':
    s = [1, 2, 3, 52, 2, 3, 1, 3, 5, 6, 2, 2, 12, 12, 12353, 64, 75, 75]
    print(select_sort(s))
# 冒泡排序
def bubble_sort(items, comp=lambda x, y: x > y):
    items = items[:]
    for i in range(len(items) - 1):
        swapped = False
        for j in range(len(items) - 1 - i):
            if comp(items[j], items[j + 1]):
                items[j], items[j + 1] = items[j + 1], items[j]
                swapped = True
        if not swapped:
            break
    return items


if __name__ == '__main__':
    s = [1, 2, 3, 52, 2, 3, 1, 3, 5, 6, 2, 2, 12, 12, 12353, 64, 75, 75]
    print(bubble_sort(s))

# 搅拌排序(冒泡排序升级版)
def bubble_sort(items, comp=lambda x, y: x > y):
    items = items[:]
    for i in range(len(items) - 1):
        swapped = False
        for j in range(len(items) - 1 - i):
            if comp(items[j], items[j + 1]):
                items[j], items[j + 1] = items[j + 1], items[j]
                swapped = True
        if swapped:
            swapped = False
            for j in range(len(items) - 2 - i, i, -1):
                if comp(items[j - 1], items[j]):
                    items[j], items[j + 1] = items[j + 1], items[j]
                    swapped = True
        if not swapped:
            break
    return items


if __name__ == '__main__':
    s = [1, 2, 3, 52, 2, 3, 1, 3, 5, 6, 2, 2, 12, 12, 12353, 64, 75, 75]
    print(bubble_sort(s))
# 归并排序
def merge(items1, items2, comp=lambda x, y: x > y):
    '''将2个有序的列表合并成一个有序的列表,生成的序列为从大到小排列'''
    items = []
    index1, index2 = 0, 0
    while index1 < len(items1) and index2 < len(items2):
        if comp(items1[index1], items2[index2]):
            items.append(items1[index1])
            index1 += 1
        else:
            items.append(items2[index2])
            index2 += 1
    items += items1[index1:]
    items += items2[index2:]
    return items


def merge_sort(items, comp=lambda x, y: x > y):
    return _merge_sort(list(items), comp)


def _merge_sort(items, comp):
    if len(items) < 2:
        return items
    mid = len(items) // 2
    left = _merge_sort(items[:mid], comp)
    right = _merge_sort(items[mid:], comp)
    return merge(left, right, comp)


if __name__ == '__main__':
    s = [1, 2, 3, 52, 2, 3, 1, 3, 5, 6, 2, 2, 12, 12, 12353, 64, 75, 75]
    print(merge_sort(s))

查找算法(顺序和折半)

# 顺序查找,查找列表中第一次出现该元素的位置
def seq_search(items, key):
    for index, item in enumerate(items):
        if item == key:
            return index
    return -1


if __name__ == '__main__':
    s = [1, 2, 3, 52, 2, 3, 1, 3, 5, 6, 2, 2, 12, 12, 12353, 64, 75, 75]
    print(seq_search(s, 2))
# 折半查找,元素第一次出现的位置
def bin_search(items, key):
    start, end = 0, len(items) - 1
    while start <= end:
        mid = (start + end) // 2
        if key > items[mid]:
            start = mid + 1
        elif key < items[mid]:
            end = mid - 1
        else:
            return mid
    return -1


if __name__ == '__main__':
    s = [1, 2, 3, 52, 2, 3, 1, 3, 5, 6, 2, 2, 12, 12, 12353, 64, 75, 75]
    print(bin_search(s, 2))

猜你喜欢

转载自blog.csdn.net/Nicky_Zheng/article/details/109012419