Python3查找算法01 - 顺序查找

  • 原创
  • Madman
  • /
  • /
  • 0
  • 8591 次阅读

Synopsis: 先介绍一种最低效的查找算法,顺序查找,它适合于存储结构为顺序存储或链接存储的线性表。无序查找是被查找数列有序无序均可,有序查找是被查找数列必须为有序数列。输入的数列如果是有序的,我们可以稍微改进一下这个算法的效率

代码已上传到 https://github.com/wangy8961/python3-algorithms ,欢迎star

1. 顺序查找

顺序查找(Sequential Search)也称为线性查找,它从线性表的一端开始,顺序扫描,依次将扫描到的元素值与查找值比较,如果相等则表示查找成功,直接返回True;如果不等则继续往后遍历,当遍历完整个线性表还没有找到,则表示查找失败,返回False

2. 查找无序数列

def sequential_search(L, item):
    '''顺序查找,输入的列表是无序的'''
    pos = 0
    found = False  # 标记是否已找到数据项

    while pos < len(L) and not found:
        if L[pos] == item:  # 表示找到了
            found = True
        else:
            pos += 1

    return found


if __name__ == '__main__':
    L1 = [54, 26, 
                                
                            
  • John
  • 邱晨100
  • nanber-WQ
  • gaofei
  • ChenJanson
  • Get an On the Spot Cash Win cr077152.tw1.ru 6Q
  • Derelict money has been rightfully collected cr077152.tw1.ru MG
  • A reserve of cash kept private for you cr077152.tw1.ru Eq
  • Act fast Your loyalty cash is expiring ci519550.tw1.ru gB
  • Your cash dispatch is on hold ygjhbgmail.temp.swtest.ru Vv
  • Qualify for Support and More Cash ygjhbgmail.temp.swtest.ru FL
  • Collect Dividends Effortlessly ygjhbgmail.temp.swtest.ru XU
  • Your Cash Prize Awaits   Get It Now ygjhbgmail.temp.swtest.ru Rv
  • Play games and win cash instantly ygjhbgmail.temp.swtest.ru xP
  • Open for an instant cash bonus ygjhbgmail.temp.swtest.ru Vh
  • Be Awarded for Your Dynamism cr077152.tw1.ru Fv
  • Capitalize on the Stock Market ci519550.tw1.ru y7
  • Easily Secure Your Monetary Payout cr077152.tw1.ru CX
  • This reward is completely unique ci519550.tw1.ru 2U
  • Quick cash victories through play cr077152.tw1.ru ji
  • You've been selected to receive free money cr077152.tw1.ru Af
  • Access Covert Savings and Cash Prizes cr077152.tw1.ru hB
  • You could win a massive prize ygjhbgmail.temp.swtest.ru lS
  • Pick Up Your Cash Incentive Now cr077152.tw1.ru nj
未经允许不得转载: LIFE & SHARE - 王颜公子 » Python3查找算法01 - 顺序查找

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列