Skip to content

Latest commit

 

History

History
51 lines (45 loc) · 1.19 KB

File metadata and controls

51 lines (45 loc) · 1.19 KB

顺序搜索的代码实现


'''
@Author: Goog Tech
@Date: 2020-07-12 21:05:54
@Description: sequential search
@FilePath: \leetcode-googtech\docs\算法入门必须知识点\Python语言实现\搜索\顺序搜索\SequentialSearch.py
'''
class SequentialSearch:
    
    '''
    @description: 无序列表的顺序搜索
    @param {alist,item} 
    @return: bool
    '''
    def unorderedList(self,alist, item):
        pos = 0;
        found = False
        while pos < len(alist) and not found:
            if alist[pos] == item:
                found = True
            else:
                pos = pos + 1
        return found


    '''
    @description: 有序列表的顺序搜索
    @param {alist,item} 
    @return: bool
    '''
    def orderedList(self,alist,item):
        pos = 0;
        found = False
        stop = False
        while pos < len(alist) and not found and not stop:
            if alist[pos] == item:
                found = True
            else:
                pos = pos + 1
        return found

s = SequentialSearch()
print(s.unorderedList([1,2,3,4,5,6],7)) # False
print(s.orderedList([1,2,3,4,5,6],6)) # True