-
Notifications
You must be signed in to change notification settings - Fork 28
/
linearsearch.html
191 lines (146 loc) · 13.6 KB
/
linearsearch.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- <title>Document</title> -->
<title>Linear Search</title>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
crossorigin="anonymous"
/>
<link
href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="src/home/css/style.css" />
<link rel="stylesheet" href="src/home/css/morebutton.css" />
<link rel="stylesheet" href="src\seive_Of_Eratosthenes\style.css" />
<script src="https://use.fontawesome.com/7ade57373d.js"></script>
<!-- Page icon -->
<link rel="icon" href="./src/assets/logo.png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>
<header>
<div id="header-inner">
<a href="#" id="logo"></a>
<a class="navbar-brand1" rel="home" href="#" title="Structurex">
<img src="src/assets/nlogo.png" class="main-logo">
<href="#" class="name">Structurex</a>
<div class="container1 blue" id="navbarNav">
<!-- ------HAMBURGER MENU -------->
<!-- -----------NEW CHANGES----------- -->
<i class="fas fa-2x fa-bars" style="color:white;display: none;" onclick="display();"></i>
<ul class="heck" id="heck">
<a href="index.html" class="link">Home</a>
<a href="sort.html" class="link">Sorting</a>
<a href="search.html" class="link">Searching</a>
<a href="./Path Finding/index.html" class="link">Path Finding</a>
<a href="tree.html" class="link" class="link">Graph</a>
<div class="dropdown">
<button onclick="show_hide()" class="Morebutton" > MORE</button>
<center>
<div id="list-items">
<!-- Add more list items here if required -->
<a href="seiveErato.html">Sieve Of Eratosthenes</a>
</div>
</center>
</div>
</ul>
</div>
</div>
</header>
<div class="algo">
<br>
<br>
<h1>Linear Search </h1>
<p>Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.</p>
<br>
<br>
<div class="box1">
<div class="Process">
<h1>How Linear Search works</h1>
<img src="https://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif" alt="Linear search Animation">
<div class ="algorithm">
<h3>Algorithm</h3>
<b>Step 1 −</b>Set i to 1<br>
<b>Step 2 −</b> if i > n then go to step 7<br>
<b>Step 3 −</b> if A[i] = x then go to step 6 <br>
<b>Step 4 − </b>Set i to i + 1<br>
<b>Step 5 − </b> Go to Step 2 <br>
<b>Step 6 −</b> Print Element x Found at index i and go to step 8 <br>
<b>Step 7 − </b>Print element not found <br>
<b>Step 8 −</b> Exit <br>
</p>
</div>
</div>
<div class = "code">
<pre class="prettyprint notranslate prettyprinted" style=""><span class="com">#include</span><span class="pln"> </span><span class="str"><stdio.h></span><span class="pln">
</span><span class="com">#define</span><span class="pln"> MAX </span><span class="lit">20</span><span class="pln">
</span><span class="com">// array of items on which linear search will be conducted.</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> intArray</span><span class="pun">[</span><span class="pln">MAX</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">3</span><span class="pun">,</span><span class="lit">4</span><span class="pun">,</span><span class="lit">6</span><span class="pun">,</span><span class="lit">7</span><span class="pun">,</span><span class="lit">9</span><span class="pun">,</span><span class="lit">11</span><span class="pun">,</span><span class="lit">12</span><span class="pun">,</span><span class="lit">14</span><span class="pun">,</span><span class="lit">15</span><span class="pun">,</span><span class="lit">16</span><span class="pun">,</span><span class="lit">17</span><span class="pun">,</span><span class="lit">19</span><span class="pun">,</span><span class="lit">33</span><span class="pun">,</span><span class="lit">34</span><span class="pun">,</span><span class="lit">43</span><span class="pun">,</span><span class="lit">45</span><span class="pun">,</span><span class="lit">55</span><span class="pun">,</span><span class="lit">66</span><span class="pun">};</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> printline</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> count</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">i </span><span class="pun"><</span><span class="pln">count</span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span><span class="pln">i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"="</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"=\n"</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="com">// this method makes a linear search. </span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> find</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> data</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> comparisons </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> index </span><span class="pun">=</span><span class="pln"> </span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
</span><span class="com">// navigate through all items </span><span class="pln">
</span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">i</span><span class="pun"><</span><span class="pln">MAX</span><span class="pun">;</span><span class="pln">i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="com">// count the comparisons made </span><span class="pln">
comparisons</span><span class="pun">++;</span><span class="pln">
</span><span class="com">// if data found, break the loop</span><span class="pln">
</span><span class="kwd">if</span><span class="pun">(</span><span class="pln">data </span><span class="pun">==</span><span class="pln"> intArray</span><span class="pun">[</span><span class="pln">i</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
index </span><span class="pun">=</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"Total comparisons made: %d"</span><span class="pun">,</span><span class="pln"> comparisons</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> index</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> display</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"["</span><span class="pun">);</span><span class="pln">
</span><span class="com">// navigate through all items </span><span class="pln">
</span><span class="kwd">for</span><span class="pun">(</span><span class="pln">i </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">i</span><span class="pun"><</span><span class="pln">MAX</span><span class="pun">;</span><span class="pln">i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"%d "</span><span class="pun">,</span><span class="pln">intArray</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"]\n"</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"Input Array: "</span><span class="pun">);</span><span class="pln">
display</span><span class="pun">();</span><span class="pln">
printline</span><span class="pun">(</span><span class="lit">50</span><span class="pun">);</span><span class="pln">
</span><span class="com">//find location of 1</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> location </span><span class="pun">=</span><span class="pln"> find</span><span class="pun">(</span><span class="lit">55</span><span class="pun">);</span><span class="pln">
</span><span class="com">// if element was found </span><span class="pln">
</span><span class="kwd">if</span><span class="pun">(</span><span class="pln">location </span><span class="pun">!=</span><span class="pln"> </span><span class="pun">-</span><span class="lit">1</span><span class="pun">)</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"\nElement found at location: %d"</span><span class="pln"> </span><span class="pun">,(</span><span class="pln">location</span><span class="pun">+</span><span class="lit">1</span><span class="pun">));</span><span class="pln">
</span><span class="kwd">else</span><span class="pln">
printf</span><span class="pun">(</span><span class="str">"Element not found."</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span></pre>
</div>
</div>
<p style=" margin-left: 50%;"> If we compile and run the above program, it will produce the following result −</p>
<h2 style=" margin-left: 50%;">Output</h2>
<div class="output">
<pre class="result notranslate">Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66 ]
==================================================
Total comparisons made: 19
Element found at location: 19
</pre>
</div>
</div>
<script src="src/home/js/morebutton.js"></script>
<script src="src/home/js/script.js"></script>
</body>
</html>