Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

UNIT 3 Linear-Search-And-Binary-Search

jn jknmjkn,knk,nj

Uploaded by

devendratavhare6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

UNIT 3 Linear-Search-And-Binary-Search

jn jknmjkn,knk,nj

Uploaded by

devendratavhare6
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Searching and

Sorting
Linear Search

• Linear Search is the simplest searching algorithm.


• It traverses the array sequentially to locate the required element.
• It searches for an element by comparing it with each element of the
array one by one.
So, it is also called as Sequential Search.
Linear Search Algorithm is applied when-
• No information is given about the array.
• The given array is unsorted or the elements are unordered.
• The list of data items is smaller.
Linear Search
Algorithm

• Consider-
• There is a linear array ‘a’ of size ‘n’.
• Linear search algorithm is being used to search an element ‘item’
in this linear array.
• If search ends in success, it sets loc to the index of the element
otherwise it sets loc to -1.
• Then, Linear Search Algorithm is as follows-
Begin
for i = 0 to (n - 1) by 1 do
if (a[i] = item) then
set loc = i
Exit
endif
endfor
set loc = -1
End
Advantages

 The linear search is simple - It is very easy to understand and


implement
 It does not require the data in the array to be stored in any
particular order
Disadvantages

 It has very poor efficiency because it takes lots of comparisons to find a


particular record in big files
 The performance of the algorithm scales linearly with the size of the
input
 Linear search is slower then other searching algorithms
Linear Search
Example

-23 97 18 21 5 -86 64 0 -37

elemen
t

Searching for 31.


7
Linear Search
Example

-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.


8
Linear Search
Example

-23 97 18 21 5 -86 64 0 -37

elemen
t

Searching for -86.


9
Linear Search
Example

-23 97 18 21 5 -86 64 0 -37

elemen
t

Searching for -86.


10
Linear Search
Example

-23 97 18 21 5 -86 64 0 -37

elemen
t

Searching for -86.


11
Linear Search
Example

-23 97 18 21 5 -86 64 0 -37

elemen
t

Searching for -86: found!


12
Analysis of Linear
Search

How long will our search take?

In the best case, the target value is in the first element of the array.
So the search takes some tiny, and constant, amount of time.
In the worst case, the target value is in the last element of the array.
So the search takes an amount of time proportional to the length of the
array.
Analysis of Linear
Search

In the average case, the target value is somewhere in the array.


In fact, since the target value can be anywhere in the array, any
element of the array is equally likely.
So on average, the target value will be in the middle of the
array.
So the search takes an amount of time proportional to half the
length of the array
Sentinel Search

 Sentinel Linear Search : Here the idea is to reduce the number of


comparisons required to find an element in a list. Here we replace the
last element of the list with the search element itself and run a while
loop to see if there exists any copy of the search element in the list and
quit the loop as soon as we find the search element. See the code
snippet for clarification.
 int last = array[N-1];
 array[N-1] = item; // Here item is the search element
 int i = 0;
 while(array[i]!=item) { i++; }
 array[N-1] = last;
 if( (i < N-1) || (item == array[N-1]) )
 { cout << " Item Found @ "<<i; }
 else { cout << " Item Not Found"; }
 Here we see that the while loop makes only one comparison in each
iteration and it is sure that it will terminate since the last element of the list
is the search element itself. So in the worst case ( if the search element
does not exists in the list ) then there will be at most N+2 comparisons
( N comparisons in the while loop and 2 comparisons in the if condition).
Which is better than ( 2N+1 ) comparisons as found in Simple Linear Search.
 Take note that both the algorithms have time complexity of O(n).
Q.1. Suppose you are doing a sequential search of the
list{15,18,2,19,18,0,8,14,19,14} How many comparisons
would you need to do in order to find the key 18?
a. 5 b. 10 c. 4 d. 2
 Q.2. Suppose you are doing a sequential search of the
ordered list{3,5,6,8,11,12,14,15,17,18} How many
comparisons would you need to do in order to find the
key 13?
a. 10 b.5 c.7 d.6
Binary
Search

The general term for a smart search through sorted data is a binary search.
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. If you’ve found your target, stop.
4. If your target is less than the middle data value, the new search region is
the lower half of the data.
5. If your target is greater than the middle data value, the new
search region is the higher half of the data.
6. Continue from Step 2.
Algorithm
Begin
 Set beg = 0
 Set end = n-1
 Set mid = (beg + end) / 2
 while ( (beg <= end) and (a[mid] ≠ item) ) do
 if (item < a[mid]) then
 Set end = mid - 1
 else
 Set beg = mid + 1
 endif
 Set mid = (beg + end) / 2
 endwhile
 if (beg > end) then
 Set loc = -1
Binary Search
Example

-86 -37 -23 0 5 18 21 64 97

lo middl hig
w e h

Searching for
21
18.
Binary Search
Example

-86 -37 -23 0 5 18 21 64 97

lo hig
w h
middl
e
Searching for
22 18.
Binary Search
Example

-86 -37 -23 0 5 18 21 64 97

lo hig
wmiddleh

Searching for 18: found!


1
7
Time Complexity of Binary
Search
How fast is binary search?
Think about how it operates: after you examine a value, you cut the search region in
half.
So, the first iteration of the loop, your search region is the whole
array.
The second iteration, it’s half the array.
The third iteration, it’s a quarter of the array....
The kth iteration, it’s (1/2k-1) of the array. So final complexity will be
Time Complexity of Binary Search Algorithm is O(log2n).
Here, n is the number of elements in the sorted linear array.
24

You might also like