Searching Operation on
Array
Searching
• The process of finding a particular element in an array is called searching.
• There two popular searching techniques:
• Linear search
• Binary search.
Linear Search
• The linear search compares each array element with the search key.
• If the search key is a member of the array, typically the location of the
search key is reported to indicate the presence of the search key in the
array.
• Otherwise, a sentinel value is reported to indicate the absence of the
search key in the array.
• Each member of the array is visited until the search key is found.
Linear Search
• Array arr contains:
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6]
17 23 5 11 2 29 3
• Searching for the value 11, linear search examines 17, 23, 5, and 11
• Searching for the value 7, linear search examines 17, 23, 5, 11, 2, 29,
and 3.
Linear Search
• (Linear search) Linear (data, N, Item, Loc)
• Here Data is a linear array with N elements and Item is a given item of
information. This algorithm finds the location LOC of Item in Data, or sets
Loc=0 if the search is unsuccessful.
1. [initialize counter] Set LOC=1
2. [Search for ITEM]
Repeat while Data[Loc] ≠Item:
Set Loc=Loc+1
[End of Loop]
3. [Successful?] If Loc = N, then Set Loc=0
4. Exit
Linear Search: Worst Case Time Complexity
• The Computational Complexity of the Linear Search is the maximum number of
comparisons you need to search the array.
• As you are visiting all the array elements in the worst case, then, the number of
comparisons required is: O(n), where n is the size of the array.
• Consider cases where we must loop over all n elements:
• desired element appears in the last position of the array
• desired element does not appear in the array at all
• Example: If a given an array of 1024 elements, then the maximum number of
comparisons required is: n = 1024 (As many as 1024 comparisons may be
required)
Linear Search: Average Case Time
Complexity
Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
• We have an array of 10 records.
• If search for the first record, then it requires 1 array access; if the second, then
2 array accesses. etc.
The average of all these searches is:
Avg case time= all possible case time/number of cases
= (1+2+3+4+5+6+7+8+9+10)/10 = 5.5
Linear Search: Average Case Time
Complexity
• Generalize for array size n.
• Expression for average-case running time:
(1+2+…+n)/n = (n(n+1)/2)/n = (n+1)/2
• Therefore, average case time complexity for serial search is O(n).
Linear Search
• Benefits:
• Easy algorithm to understand
• Array can be in any order
• Disadvantages:
• Inefficient (slow): for array of N elements, examines N/2 elements on average
for value in array, N elements for value not in array.
Binary Search
• Binary search is a smart algorithm that is much more efficient than the linear
search. The elements in the array must be sorted in order.
• Divides the array into three sections:
• middle element
• elements on one side of the middle element
• elements on the other side of the middle element
• Instead of searching the array’s first element, it starts with the element in the
middle.
• If the middle element happens to be the specific item we want to search, then the
search is over.
Binary Search
• The middle element is greater than the specific item, then the desired item will be
found somewhere in the first half of the array.
• If it is less, then the desired item will be found somewhere in the second half of
the array. In either cases, half of the array’s elements have been eliminated from
the further searching.
• The procedure is repeated for the half of the array that potentially contains the
value.
Binary Search
• Array arr contains:
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6]
2 3 5 11 17 23 29
• Searching for the value 11, binary search examines 11 and stops
• Searching for the value 7, linear search examines 11, 3, 5, and stops
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Find approximate midpoint
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Is 7 = midpoint element? NO.
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Is 7 < midpoint element? YES.
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Search for the target in the area before midpoint.
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Find approximate midpoint
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Target = element of midpoint? NO.
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Target < element of midpoint? NO.
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Search for the target in the area after midpoint.
Binary Search
Example: sorted array of integer elements. Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
Find approximate midpoint.
Is target = midpoint element? YES.
Binary Search
• (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,
respectively. The beginning, end and middle location of a segment of elements of
Data. This algorithm finds the location Loc of Item in Data or sets Loc = Null
Binary Search
• (Binary Search) Binary(Data, LB, UB, Item, Loc)
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 Structure]
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 Structure]
6. Exit
Binary Search
• The Computational Complexity of the Binary Search algorithm is measured by the
maximum (worst case) number of comparisons it performs for searching
operations.
• The searched array is divided by 2 for each comparison/iteration.
• Therefore, the maximum number of comparisons is measured by: floor(log2n), and
worst case complexity is O(log2n), where n is the size of the array.
• Example: If a given sorted array 1024 elements, then the maximum number of
comparisons required is: log2(1024) = 10 (only 10 comparisons is enough)
Binary Search
• Benefits:
• Much more efficient than linear search. For array of N elements, performs at
most log2N comparisons.
• Disadvantages:
• Requires that array elements be sorted