Skip to content

Commit 7d1affb

Browse files
authored
Create LinearSearch.md
1 parent 4e94733 commit 7d1affb

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
## Search Algorithm
2+
3+
```
4+
1.Linear Search
5+
2.Binary Search
6+
```
7+
8+
### 1.Linear Search
9+
- Linear search is also called as sequential search
10+
- It sequentially checks each element of the list until a match is found or the whole list has been searched if element is not found.
11+
- But linear search rearly used in practical scenario.
12+
13+
#### Advantages
14+
- When a key element matches the first element in the array then this is best case Scenario.
15+
- Linear Search Also Can work on unorderd list of element.
16+
17+
#### Disadvantages
18+
- When a key element matches the last element in the array or a key element doesn't matches any element then Linear search algorithm is a worst case.
19+
20+
21+
#### Algorithm
22+
23+
Consider
24+
```
25+
input Variable
26+
lst : Input List
27+
number: Search ELement
28+
29+
Output Variable
30+
result : True if Element is Found Else False
31+
index : -1 if element is not found else return index of search element.
32+
iteration : return Number of iteration
33+
```
34+
35+
**Step1** : Initialize a boolean variable result and set it to False initially , initialize index variable and set -1 initially.
36+
```python
37+
result=False
38+
index=-1
39+
```
40+
41+
**Step2** : Start for loop with i ranging from 0 to the length of the list
42+
43+
**Step3** : Check if number element is equal to lst[i] if equals
44+
- set result boolean variable to True
45+
- set index variable to i
46+
- break for loop
47+
48+
**Step4** : Return the result,index,iteration
49+
50+
51+
#### Program
52+
53+
```python
54+
def LinearSearch(lst,number):
55+
'''Function Input
56+
lst: A Integer Element List
57+
number: The Number which Do you Want to Search
58+
59+
Function Output:
60+
result: True if number is found else False
61+
index: if element found then index else return -1 if element is not found
62+
iteration : Total No of Iteration Required
63+
'''
64+
65+
result=False
66+
iteration=0
67+
index=-1
68+
69+
70+
for i in range(len(lst)):
71+
iteration+=1
72+
if lst[i]==number:
73+
result=True
74+
index=i
75+
break
76+
return result,index,iteration
77+
78+
79+
#Driver Program
80+
#Simple List
81+
lst=[1,2,3,4,5,6,7,8,9,10]
82+
83+
# Print Normal List
84+
print(lst)
85+
#Get Use Input From User
86+
number=int(input("Enter Search Number:"))
87+
88+
#Call Search Function and save result in res and No of iteration in iteration
89+
res,index,iteration=LinearSearch(lst,number)
90+
91+
#Print the Result
92+
print("\nNumber In List: {} \nNumber Index :{} \nIt Required iteration: {}".format(res,index,iteration))
93+
```
94+
95+
Output for 2 Input:
96+
```
97+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
98+
Enter Search Number: 2
99+
100+
Number In List: True
101+
Number Index :1
102+
It Required iteration: 2
103+
```
104+
105+
Output for 11 Input:
106+
```
107+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
108+
Enter Search Number: 11
109+
110+
Number In List: False
111+
Number Index :-1
112+
It Required iteration: 10
113+
```
114+
115+
Output for 5 Input:
116+
```
117+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
118+
Enter Search Number: 5
119+
120+
Number In List: True
121+
Number Index :4
122+
It Required iteration: 5
123+
```
124+
125+
#### Time Complexity for ordered Linear search
126+
**1.Best Case**
127+
- O(1)
128+
- In above Example for element 1
129+
130+
**2.Average case**
131+
- O(n)
132+
- In above Example for element 5
133+
134+
**3. Worst case**
135+
- O(n)
136+
- In above Example for element 11
137+
138+
**Reason**:
139+
We need to go from the first element to the last so,
140+
in the worst case we have to iterate through n elements, n being the size of a given array.
141+
142+
#### Space Complexity For ordered Linear Search
143+
O(1)
144+
145+
**Reason** :
146+
We don't need any extra space to store anything.We just compare the given value with the elements in an array one by one.

0 commit comments

Comments
 (0)