1 Linear Search: Algorithm Input: Output: While and Do If Then Return Else Return
1 Linear Search: Algorithm Input: Output: While and Do If Then Return Else Return
To solve the problem of deciding whether x 2 S , we must determine rst how we are going
to organize the elements of set S m so they can be stored in the memory of the computer.
The simplest approach might be to store the elements of S in an array L. Let us assume
that the positions of the array are indexed from 0 to n ? 1, as it the convention in Java,
where n is the number of elements in S . For simplicity let us assume that the elements of S
are all integers.
The simplest way of looking for a value x in L is to scan sequentially the array. Thus, we
rst compare element L[0] with x; if x = L[0] then we are done, otherwise we compare x
with L[1] and so on until either we nd x or we scan the entire array L without nding x.
This way of looking for a value in an array is called linear search.
Algorithm LinearSearch(x; L; n)
Input: Array L, value x, and number n of elements in S
Output: Position i, 0 i < n such that L[i] = x, or ?1 if x 62 S
i 0
2 Binary Search
There are many algorithms for solving a given problem, and as designers, we always look for
the best one. We will say a few words later about criteria for comparing algorithms. Right
now, let us assume that we want to solve the search problem as fast as we can. Is linear
search the best solution for the problem at hand?
Let us assume that the elements in the array L are sorted in non-decreasing order of value.
Making that assumption, can we come up with a dierent way of solving the problem? Just
think about the way in which we look up a name in the phone book. We do not (at least I
hope that you do not do this) open the phone book in the rst page, compare all the names
there against the name that we are looking for, then turn to the second page, do the same
thing, and so on until we nd the desired name.
Instead what we do is to open the phone book at some page near the middle of the book,
compare the name that we want against any name in the page and based on this comparison
decide whether we should look for the name in the succeeding or in the preceding pages. The
search is continued in the same fashion, namely, we take the pages where the name might
be, select a page and compare the name against any name from that page. When there is
only one page that might contain the name of interest, then we look for it in basically the
same manner.
We can apply the same idea when looking for a value x in array L. We compare x with the
1
element L[mid] located in the middle of the array, and: (i) if x = L[mid] then the search
ends, (ii) if x < L[mid] then we look for x in the left half of the array, (iii) if x > L[mid]
then we look for x in the right half of the array. To look for x in either half of the array
we proceed in the same manner as above. This way of searching a value in a sorted array is
called binary search
3 Comparing Algorithms
Given 2 or more algorithms that solve the same problem, how do we select the best one?
To answer this question we need to dene the criteria that we are going to use to compare
the algorithms. We might be interested in choosing the algorithm that is simpler, easier to
understand, implement, and modify. Or we might be interested in choosing the algorithm
that makes better use of the computer resources.
The rst kind of criteria, qualitative criteria, is used in Software Engineering courses, and
we will not use it here. Instead we will say that an algorithm is better than other if it
uses less computer resources. We are interested in 2 computer resources: processing time
and memory. We dene the time complexity of an algorithm as the amount of time that the
algorithm needs to complete. The space complexity of an algorithm is the amount of memory
that it needs to execute properly.
In this course we are mainly interested in computing the time complexity of algorithms. The
techniques that we describe for computing the time complexity can be used to determine
the space complexity of an algorithm. So when we talk about complexity of an algorithm
we are referring to time complexity, unless we specically state otherwise.
The time that an algorithm needs to nd a solution depends on the instance of the problem
that it is given as input. Consider for example the linear search algorithm. If the input is
an array L in which the value x is in the rst position of the array, then the algorithm will
nd the solution very quickly, while if the value x is not in L then the algorithm will require
a larger amount of time.
To consider this dependency of the time complexity on the particular instance that the
algorithm has to solve, we dene:
2
The time complexity in the best case. Measures the least amount of time that the
a. Outside the while loop, the algorithm performs one assignment (i 0).
b. At each iteration of the while loop, the algorithm performs two comparisons (i < n)
and (L[i] 6= x), one boolean and operation, one addition and one assignment i i +1.
c. After the last iteration, i has value n. So the next time that the condition of the while
loop is tested, the condition i < n is false, so the body of the loop is not executed.
d. Finally, one comparison and one return operation are performed.
Since the for loop is repeated n times, the total number of operations is
assignments:
additions:
comparisons (<):
comparisons (6=):
comparisons (=):
boolean and:
return:
4
n+1
n
n+1
n
1
n
1
Let t , t= , t+, t6=, t<, tand, and tret denote the execution times of the primitive operations.
Then, the total running time of the algorithm is