Professional Documents
Culture Documents
Table of Contents
In this algorithm, we input two parameters: the array we want to search and the item
we want to look for (being the key). The number of items in the array is n. If the item
is found in the array, then the return value of the function is its position in the array; if
the item does not occur in the list, then the return value is −1. We take the size of the
input to be n, the length of the array, and we investigate the best, worst, and average
case running time as functions of n.
The best case occurs when the item occurs as the first item in the array. In that case,
only a small, constant amount of work is done. We can say that the best case running
time is Θ(1).
The worst case occurs when the item is not in the array. In that case, the for loop exe-
cutes n times, so the worst case running time is Θ(n). If we assume that each item in
the array has an equal chance of being searched for, then the for loop is executed an
average of n/2 times for items in the array, giving an average case running time of Θ
(n) for items in the array.
Mathematically for an Θ(n) function we obtain the following recurrence relation:
T(n) = T(n-1) + Θ(1).
The startIndex increases from n to n + 1 at every stage until it is equal to the endIn-
dex and therefore the range between startIndex and endIndex decreases from n to n-1
until it is equal to 0, and at every stage two operations are involved (one logic com-
parison and one recursive call). Thus total number of operations needed to execute
the function for any given n, can be expressed as sum of 2 operations and the total
number of operations needed to execute the function for n-1. Also when n=1, it just
needs one operation to execute the function
Thus
Therefore,
TafRecursiveLinearSearch(n) = Θ(1) + TafRecursiveLinearSearch(n-1)
In the best case, the item is found immediately, and the amount of work that is done is
constant. So, the best case running time is Θ(1). The worst case occurs when the item
is not in the array.
Each time through the loop, the difference between upper and lower is reduced to a
little less than half its previous value. When this difference is reduced to zero, the
loop ends. Initially, upper − lower is n. We are concerned in finding how many times
we need to divide this value by 2 before we get to zero. Mathematically, this is given
by the integer part of the function log2(n). So, in the worst case, the while loop is
executed about log2(n) times, and the worst case running time is Θ(log2(n)). For all
but very small values of n, log2(n) is much smaller than n, which means that binary
search is much more efficient than linear search at least for sufficiently large sets of
data.
The recurrence relation for the running time of the method is:
T(1) = a if n = 1 (one element array)
T(n) = T(n / 2) + b if n > 1
Expanding:
T(n) = T(n / 2) + b
= [T(n / 4) + b] + b = T (n / 22) + 2b
= [T(n / 8) + b] + 2b = T(n / 23) + 3b
= ……..
= T( n / 2k) + kb
/**
* Data Structures and Algorithms
* Coursework Part 1
* Author: Alessio Failla - af128
* Date: 05/10/2010
*
* Comments:
* This class implements some basic search algorithms
* on an array of type Book as required in the
* coursework specification.
*
*/
static Book[]books; // the array of type Book containing the
// book's information.
public static void main(String[] args) {
books=new Book[2];
books[0]=new Book("Book1",1999,"1234-5678","John Smith");
books[1]=new Book("Book2",2005,"1234-5678","John Smith");
//System.out.println(books[0].getYear());
System.out.println("Linear Search Result: :" + afLinearSear-
ch(books,1999));
System.out.println("Recursive Linear Search Result :" + afRe-
cursiveLinearSearch(books,1999,0,2));
System.out.println("Binary Search Result: "+afBinarySear-
ch(books,1999));
System.out.println("Recursive Binary Search result: "+afRecur-
siveBinarySearch(books,0,2,1999));
}