简单实用算法

2018-12-10  本文已影响0人  Ming_a221
# 展开数组
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
上一篇下一篇

猜你喜欢

热点阅读