美文网首页
python数据结构和算法

python数据结构和算法

作者: Yimmy_Lee | 来源:发表于2020-03-25 22:24 被阅读0次
'''
1. 数据结构和算法
排序算法(冒泡和归并)和查找算法(顺序和折半)
'''


def bubble_sort(origin_items, comp=lambda x, y: x > y):
    """⾼质量冒泡排序(搅拌排序)"""
    items = origin_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


# 归并排序(分治法)
def merger_sort(items, comp=lambda x, y: x <= y):
    """归并排序"""
    if len(items) < 2:
        return items[:]
    mid = len(items) // 2
    left = merger_sort(items[:mid], comp)
    right = merger_sort(items[mid:], comp)
    # print(merge(left, right, comp))
    print("this is left and right.")
    print(left, right)
    return merge(left, right, comp)


def merge(item1, item2, comp):
    items = []
    idx1, idx2 = 0, 0
    while idx1 < len(item1) and idx2 < len(item2):
        if comp(item1[idx1], item2[idx2]):
            items.append(item1[idx1])
            idx1 += 1
        else:
            items.append(item2[idx2])
            idx2 += 1
    items += item1[idx1:]
    items += item2[idx2:]
    # print(items)

    return items


# 顺序查找
def seq_search(items, key):
    for index, num in enumerate(items):
        if num == key:
            return index
    return -1


# 折半查找  有序数组中查找某一特定元素的搜索算法。
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__":
    items_test = [2]
    ls = [34, 12, 12, 4, 98, 5, 6]
    # print(ls[:])
    print(merger_sort(ls))  # [4, 5, 6, 12, 12, 34, 98]

本文链接:https://www.jianshu.com/p/1bddf3bf67b4

相关文章

  • 个人 Python 书单

    入门: Beginning Python 数据结构: Python 数据结构 算法: Python 算法教程

  • 10.数据结构和算法 初识

    1、数据结构与算法(Python) 数据结构和算法是什么?答曰:兵法! 1.1算法的概念 算法是计算机处理信息的本...

  • python数据结构与算法总结

    python常用的数据结构与算法就分享到此处,本月涉及数据结构与算法的内容有如下文章: 《数据结构和算法对pyth...

  • python学习资料

    算法:Python中数据结构和算法的最小例子https://github.com/keon/algorithms?...

  • 数据结构和算法(六)

    1. Python 内置数据结构和算法 使用过哪些内置的常用的算法和数据结构 sorted:排序 dict、lis...

  • 数据结构和算法对python意味着什么?

    数据结构和算法对于python而言是他的灵魂;程序是数据结构加上算法来实现的,对于任何一门编程语言都离不开数据结构...

  • 数据结构与算法基本概念

    数据结构与算法 本文包括: 算法概念 时间复杂度 大 O 记法 数据结构概念 Python 内置类型的效率 算法的...

  • Python语言进阶

    Python语言进阶 数据结构和算法 算法:解决问题的方法和步骤 评价算法的好坏:渐近时间复杂度和渐近空间复杂度。...

  • 首位交易循环机制(TTCM)的Python实现

    Xc.LiFeb.2016 算法原理 首位交易循环机制的算法,略。 Python实现 学校和学生的数据结构 学校 ...

  • python面试算法

    python内置数据结构算法常考 常用的内置算法和数据结构 sorted排序函数 dict/list/set/tu...

网友评论

      本文标题:python数据结构和算法

      本文链接:https://www.haomeiwen.com/subject/lasfuhtx.html