Anda di halaman 1dari 12

Linear and Binary Search

Linear Search
• The sequential search (also called the linear search) is the
simplest search algorithm when we have no knowledge about
the array that whether it is sorted or not.
• It is also the least efficient.
• It simply examines each element sequentially, starting with the
first element, until it finds the key element or it reaches the end
of the array.
Example: If you were looking for someone on a moving
passenger train, you would use a sequential search.
Algorithm
A linear array DATA with N elements and a specific
ITEM of information are given. This algorithm finds the
location LOC of ITEM in the array DATA or sets LOC = 0.

1. Set DATA[N+1]= ITEM [Insert item at end of DATA]


2. Set LOC=1 [Initialize counter]
3. Repeat while DATA[LOC]!= ITEM [Search for item]
Set LOC=LOC+1
[End loop]
4. [Successful?]If LOC=N+1,then set LOC=0.
5. Exit.
Complexity Analysis of Linear Search
• In a searching algorithm the key operation is comparison. So, in a searching
algorithm complexity can be measured by counting the number of
comparisons.
➢ Worst Case: When we find the element at last position ie nth position of the
array or we could not locate the element in the array. So, worst case can be
expressed as f(n)=n.
➢ Average Case: Here we consider the probability of presence of given data
element at any position in the array. If we find the element at 1st position, one
comparison takes place or when the data element is found at 2nd position then
two comparisons takes place. In general, we can say that if element is found at
kth position then there will be k number of comparisons.
f(n)= (1+2+3+4+.......n)/n
= (n*(n+1)/2)/n
= n(n+1)/2n
=(n+1)/2 ie comparisons will be half the number of elements in the array.
➢ Best Case: When the element is found at the 1st position.So, best case can be
expressed as f(n)=1.
The Binary Search Algorithm
• Applied on an array when the elements in the array are sorted
alphabetically in increasing or decreasing order.
• It is much more efficient than the sequential search, but it
does require that the elements to be in order.
• It repeatedly divides the sequence in two, each time
restricting the search to the half that would contain the
element.
• You might use the binary search to look up a word in a
dictionary. While finding a word in a dictionary we do not
search the word in linear order ie word by word from
beginning to end. Instead we select the portion of dictionary
that contains the word(first half or second half).
Conditions
• The algorithm must fulfill the following conditions:
1. List must be sorted.
2. One must have direct access to middle element of the list.

Disadvantage
1. Keeping data in sorted array is very expensive when there are many
insertions and deletions.
Algorithm
(Binary search) BINARY(DATA, LB, UB, ITEM, LOC)
Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is
a given item of information. The variables BEG, END and MID denote, resp., the
beginning, end, and middle locations of a segment of elements of DATA. The algo
finds the location LOC of ITEM in DATA or sets LOC = NULL.

1. [Initialize segment variables.]


Set BEG := LB, END := UB and MID := Int((BEG + END) / 2).
2. Repeat steps 3 and 4 while BEG <= END and DATA[MID] != ITEM.
3. If ITEM < DATA[MID], then:
Set END := MID – 1.
Else: Set BEG := MID + 1.
[End of If.]
4. Set MID := INT((BEG + END)/2).
[End of Step 2 loop.]
5. If DATA[MID] = ITEM, then:
Set LOC := MID.
Else: Set LOC := NULL.
[End of If.]
6. Exit.
Example: Binary Search
• Q:- Let DATA be the following sorted 13
element array:
DATA:
11,22,30,33,40,44,55,60,66,77,80,88,99.
Now suppose the item to be searched is 40.
Complexity Analysis of Binary Search
• Best Case: When the element being searched for is exactly at the middle of
sorted array. So, only one comparison is required to find the desired
element. Running time for best case is O(1).
• Worst Case: For the worst case, let us say we want to search for the
number 13.

• Selecting the middle element as pivot (length / 2)


• Since 13 is less than pivot, we
remove the other half of the array

• Repeating the process for finding


the middle element for every
sub-array.
• We can see that after every
comparison with the middle
term, our searching range gets
divided into half of the current
range.
• So, for reaching one element
from a set of 16 elements, we
had to divide the array 4 times,
• Similarly, for n elements

• Separating the power of


numerator and denominator
• Multiplying both sides by 2^k.

• Final Result
• definition of logarithm, says that:
“A quantity representing the power to
which a fixed number (the base) must
be raised to produce a given number.”
• Which makes our equation into
logarithmic form.
• Average Case: Here, again the number
of comparisons is O(log n), because it
halves it halves the array until it is able
to get the element in the array.
Comparison between Binary and
Linear search
Binary search operates on sorted lists. Linear search can operate on unsorted
lists as well.
Binary search is complex as compared to Linear search is simple and
linear search. straightforward to implement than the
binary search.
Binary search is considered to be a more But, linear search is too slow to be used
efficient method that could be used with with large lists due to its O(n) average
large lists. case performance.
Number of comparisons are less. More number of comparisons are
required if the items are present in the
later part of the array or its elements are
more.
Works well with arrays and not on linked Works with arrays and linked lists.
lists.

Anda mungkin juga menyukai