美文网首页
简单实用算法

简单实用算法

作者: Ming_a221 | 来源:发表于2018-12-10 15:46 被阅读0次
    # 展开数组
    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
    

    相关文章

      网友评论

          本文标题:简单实用算法

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