-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinearSearch.py
More file actions
91 lines (63 loc) · 1.89 KB
/
LinearSearch.py
File metadata and controls
91 lines (63 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import random
data = [random.randint(1,20) for x in range (30)]
#print(data)
def LinearSearch(data):
key = int(input('Enter value to search'))
found = False
for x in range (30):
if key == data[x]:
print("found at location: ", x)
found = True
if found==False:
print('value not found')
def BubbleSort(data):
temp = 0
n = 30 -1
for i in range (30):
for j in range(n):
if data[j]>data[j+1]:
temp = data[j]
data[j] = data [j+1]
data [j+1] = temp
n = n -1
def InsertionSort(data):
ItemToBeInserted = 0
CurrentItem = 0
index = 0
for index in range(len(data)):
ItemToBeInserted = data[index]
CurrentItem = index -1
while (data[CurrentItem]>ItemToBeInserted and
CurrentItem>-1):
data[CurrentItem+1]=data[CurrentItem]
CurrentItem-=1
data[CurrentItem+1] = ItemToBeInserted
def BinarySearch(data):
lowerBound = 0
upperBound = len(data)-1
key = int(input("Enter value to search: "))
found = False
searchFailed = False
while not searchFailed and not found:
mid = lowerBound + (upperBound - lowerBound) // 2
if data[mid]==key:
print('found at location : ', mid)
found = True
elif data[mid]>key:
upperBound = mid - 1
else:
lowerBound = mid + 1
if lowerBound > upperBound:
print('search failed')
searchFailed = True
if found:
print(mid)
else:
print('value does not exist in the list')
#BubbleSort(data)
InsertionSort(data)
print(data)
BinarySearch(data)
#LinearSearch(data)
BubbleSort(data)
print(data)