1+ #Linear Search on unsorted list
2+ def search (unsorted_list , term ):
3+ for i , item in enumerate (unsorted_list ):
4+ if item == term :
5+ return i
6+ return None
7+
8+ print ("----unsorted list----\n " )
9+
10+ list1 = [60 , 1 , 88 , 10 , 11 , 600 ]
11+ search_term = 10
12+ index_position = search (list1 , search_term )
13+ print (index_position )
14+ list2 = ['packt' , 'publish' , 'data' ]
15+ search_term2 = 'data'
16+ Index_position2 = search (list2 , search_term2 )
17+ print (Index_position2 )
18+
19+ #Linear Search on ordered list
20+ def search_ordered (ordered_list , term ):
21+ for i , item in enumerate (ordered_list ):
22+ if item == term :
23+ return i
24+ elif item > term :
25+ return None
26+ return None
27+
28+ print ('----ordered list----\n ' )
29+
30+ list1 = [2 , 3 , 4 , 6 , 7 ]
31+ search_term = 5
32+ index_position1 = search_ordered (list1 , search_term )
33+
34+ if index_position1 is None :
35+ print ("{} not found" .format (search_term ))
36+ else :
37+ print ("{} found at position {}" .format (search_term , index_position1 ))
38+
39+
40+ list2 = ['book' ,'data' ,'packt' , 'structure' ]
41+ search_term2 = 'structure'
42+ index_position2 = search_ordered (list2 , search_term2 )
43+ if index_position2 is None :
44+ print ("{} not found" .format (search_term2 ))
45+ else :
46+ print ("{} found at position {}" .format (search_term2 , index_position2 ))
0 commit comments