简单实用算法

# 展开数组
def exportList(ls):
    return sum([exportList(x) if type(x) is list else [x] for x in ls], [])


def li(ls, tem=[]):
    for l in ls:
        if type(l) is list:
            li(l, tem)
        else:
            tem.append(l)
    return tem


# 二分查找
def twoSerch(ls, x, low, hight):
    if (low <= hight):
        mid_index = int((low + hight) / 2)
        mid = ls[mid_index]
        if mid == x:
            return mid_index
        elif mid < x:
            return twoSerch(ls, x, mid_index + 1, hight)
        elif mid > x:
            return twoSerch(ls, x, low, mid_index - 1)
    else:
        print(-1)
        return -1


# 快速排序
def quickSort(ls):
    if (ls == []):
        return ls
    else:
        first = ls[0]
        less = quickSort([x for x in ls if x < first])
        more = quickSort([x for x in ls if x > first])
        return less + [first] + more
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容