zoukankan      html  css  js  c++  java
  • 二分法查找(Python版)

    代码
    #! /usr/bin/env python
    #
     coding=utf-8
    import random

    def dichotomy(L,K,Index):
        
    if(len(L) == 1):
            
    return Index
        
        length 
    = len(L) / 2
        hit 
    = L[length]
        
        
    if(hit == K):
            
    return Index + length
        
    else:
            
    if hit > K:
                
    return dichotomy(L[:length],K,Index)
            
    else:
                
    return dichotomy(L[length + 1:],K,Index + length + 1)
            
        
        
    if __name__ == '__main__':
        L 
    = [random.randint(1,100for i in range(1,20)]
        L 
    = {}.fromkeys(L).keys()
        L.sort()
        
        K 
    = random.choice(L)
        
    print L,K
        index 
    = dichotomy(L,K,0)
        
    print index,L.index(K)
            
            


  • 相关阅读:
    弹性盒子模型属性之flex-shrink
    Git----基本操作
    Git----简介
    ES6常用语法
    nginx学习
    Shell基础命令(二)
    Linux目录
    Shell基础命令(一)
    CRM之分页
    Django之ModelForm组件
  • 原文地址:https://www.cnblogs.com/goodspeed/p/1780790.html
Copyright © 2011-2022 走看看