数据结构之查找 --顺序查找

107 阅读1分钟
#!/usr/bin/env python2.7
# -*- conding: utf-8 -*-
# @Time : 2020/9/15 19:11
# @Author : henry
# @file : sequentialSearch.py.py
# @Project: python-script 

def sequentialSearch(datalist,item):
    pos = 0
    found = False
    while pos < len(datalist) and not found:
        if datalist[pos] == item:
            found = True
            return pos
        else:
            pos = pos + 1

    return found
'''

datalist = [10,20,30,5,1,7]
item = 7
result = sequentialSearch(datalist,item)
print result
print datalist[result]
'''

def oredersequentialSearch(datalist,item):
    pos = 0
    found = False
    stop = False
    while pos < len(datalist) and not found and not stop:
        if datalist[pos] == item:
            found = True
            return pos
        elif datalist[pos] > item:
            stop = True
        else:
            pos = pos + 1

    return found

datalist = [1,3,5,10,20,30]

print(oredersequentialSearch(datalist,7))