账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    长度为n的列表,查找出前k大的元素,这种算法的时间度是少的吗
    39
    0

    如果求前10大的元素,还有更少时间的算法吗
    纯属好奇,望大侠不吝赐教

    ###列表
    temp0 =  range(2000,5001)
    temp1 =  range(200,500)
    temp2 =  range(801,8000)
    a = sorted(temp0)
    b = sorted(temp1)
    c = sorted(temp2)
    d = b + c + a
    
    ### 计算过程
    temp = []
    t = 0
    for i in d:
        t += 1
        le = len(temp)
        if le < 10:
            temp.insert(0,i)
            temp = sorted(temp)        
        else:
            f = temp[0]
            if i > f:
                temp[0] = i
                temp = sorted(temp) 
    print("总数:",len(d))
    print("查询次数:",t)
    print(temp)
    #计算结果
    总数: 10500
    查询次数: 10500
    [7990, 7991, 7992, 7993, 7994, 7995, 7996, 7997, 7998, 7999]
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部