1. Algorithm Analysis
Searching
Dr.Haitham A. El-Ghareeb
Information Systems Department
Faculty of Computers and Information Sciences
Mansoura University
helghareeb@gmail.com
November 25, 2012
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 1 / 55
37. Searching
Searching
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 37 / 55
38. Searching
There are two fundamental ways to search for data in a list: the sequential
search and the binary search.
Sequential search is used when the items in the list are in random
order.
Binary search is used when the items are sorted in the list.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 38 / 55
39. Sequential Search
The most obvious type of search
begin at the beginning of a set of records and move through each
record until you find the record you are looking for or you come to the
end of the records.
A sequential search (also called a linear search) is very easy to
implement.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 39 / 55
40. Sequential Search
bool SeqSearch ( int [ ] arr , int sValue ) {
for ( int index = 0 ; index < arr . Length −1; index++)
i f ( arr [ index ] == sValue )
return true ;
return false ;
}
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 40 / 55
41. Modified Sequential Search
You can write the sequential search function so that the function returns
the position in the array where the searched-for value is found or a 1 if the
value cannot be found. First, lets look at the new function:
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 41 / 55
42. Modified Sequential Search Code
static int SeqSearch ( int [ ] arr , int sValue ) {
for ( int index = 0 ; index < arr . Length −1; index++)
i f ( arr [ index ] == sValue )
return index ;
return −1;
}
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 42 / 55
43. Searching for Minimum and Maximum Values
Computer programs are often asked to search a data structure for
minimum and maximum values.
In an ordered array, searching for these values is a trivial task.
Searching an unordered array, however, is a little more challenging.
Lets look at how to find the minimum value in an array. The
algorithm is:
1 Assign the first element of the array to a variable as the minimum
value.
2 Begin looping through the array, comparing each successive array
element with the minimum value variable.
3 If the currently accessed array element is less than the minimum value,
assign this element to the minimum value variable.
4 Continue until the last array element is accessed.
5 The minimum value is stored in the variable.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 43 / 55
44. FindMin Function
static int FindMin ( int [ ] arr ) {
int min = arr [ 0 ] ;
for ( int index = 1 ; index < arr . Length −1; i++)
i f ( arr [ index ] < min )
min = arr [ index ] ;
return min ;
}
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 44 / 55
45. FindMax Function
static int FindMax ( int [ ] arr ) {
int max = arr [ 0 ] ;
for ( int i = 0 ; i < arr . Length −1; i++)
i f ( arr [ index ] > max )
max = arr [ index ] ;
return max ;
}
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 45 / 55
46. Assignment
An alternative version of these two functions could return the position of
the maximum or minimum value in the array rather than the actual value.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 46 / 55
47. Self-Organizing Data
The fastest successful sequential searches occur when the data
element being searched for is at the beginning of the data set.
You can ensure that a successfully located data item is at the
beginning of the data set by moving it there after it has been found.
The concept behind this strategy is that we can minimize search
times by putting frequently searched-for items at the beginning of the
data set.
Eventually, all the most frequently searched-for data items will be
located at the beginning of the data set.
This is an example of self-organization, in that the data set is
organized not by the programmer before the program runs, but by the
program while the program is running.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 47 / 55
48. Binary Search
When the records you are searching through are sorted into order, you
can perform a more efficient search than the sequential search to find
a value.
This search is called a binary search.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 48 / 55
49. Binary Search in Action
To understand how a binary search works, imagine you are trying to
guess a number between 1 and 100 chosen by a friend.
For every guess you make, the friend tells you if you guessed the
correct number, or if your guess is too high, or if your guess is too low.
The best strategy then is to choose 50 as the first guess. If that guess
is too high, you should then guess 25. If 50 is to low, you should
guess 75.
Each time you guess, you select a new midpoint by adjusting the
lower range or the upper range of the numbers (depending on if your
guess is too high or too low), which becomes your next guess.
As long as you follow that strategy, you will eventually guess the
correct number.
Next slide demonstrates how this works if the number to be chosen is
82.
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 49 / 55
50. Binary Search in Action
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 50 / 55
51. Binary Search in Action
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 51 / 55
52. Binary Search in C#
static int binSearch ( int value ) {
int upperBound , lowerBound , mid ;
upperBound = arr . Length −1;
lowerBound = 0 ;
w h i l e ( lowerBound <= upperBound ) {
mid = ( upperBound + lowerBound ) / 2 ;
i f ( arr [ mid ] == value )
return mid ;
else
i f ( value < arr [ mid ] )
upperBound = mid − 1 ;
else
lowerBound = mid + 1 ;
}
return −1;
}
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 52 / 55
53. Recursive Binary Search
public int RbinSearch ( int value , int lower , int upper ) {
i f ( lower > upper )
return −1;
else {
int mid ;
mid = ( int ) ( upper+lower ) / 2;
i f ( value < arr [ mid ] )
RbinSearch ( value , lower , mid −1) ;
e l s e i f ( value = arr [ mid ] )
return mid ;
else
RbinSearch ( value , mid +1 , upper )
} }
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 53 / 55
54. Iterative vs. Recursive Binary Search
The main problem with the recursive binary search algorithm, as
compared to the iterative algorithm, is its efficiency.
When a 1,000-element array is sorted using both algorithms, the
recursive algorithm is consistently 10 times slower than the iterative
algorithm
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 54 / 55
55. Excercises
Just a Reminder
Dr.Haitham A. El-Ghareeb (CIS) Data Structures and Algorithms - 2012 November 25, 2012 55 / 55