You are on page 1of 10

Data Structures & Algorithms

COURSEWORK PART 1 - Asymptotic Analysis

PREPARED BY ALESSIO FAILLA - af128@hw.ac.uk

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk


Heriot-Watt University

Table of Contents

• Linear Search Asymptotic Analysis.................................................page 2


• Recursive Linear Search Asymptotic Analysis.................................page 3
• Binary Search Asymptotic Analysis ................................................page 4
• Recursive Binary Search Asymptotic Analysis ................................page 5
• Appendix A - Book Class ............................................................. page 6
• Appendix B - booksCsw Class (driver class) ................................. page 7

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 1


Heriot-Watt University

Linear Search Asymptotic Analysis

public static long afLinearSearch(Book[] Array, long key){


for(int j=0;j<Array.length;j++){
if(key == Array[j].getYear()){
return j; // item found - return its index in the array
}
}
return -1; //item not found
}

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).

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 2


Heriot-Watt University

Recursive Linear Search Asymptotic Analysis

public static long afRecursiveLinearSearch(Book[] arr, long key, int


startIndex,int endIndex) {

if (startIndex>= arr.length) { // reach the end


return -1;
} else if (key == arr[startIndex].getYear()){
return startIndex; //item found - return its index
} else
//item not found - recursively call this method in-
creasing the startIndex
return afRecursiveLinearSearch(arr, key, startIndex + 1, endIndex);

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

Let TafRecursiveLinearSearch(n) denote the computation time to execute afRecursiveLinearSearch(n).

From the above method is possible to obtain the following relations:


Θ(1).
TafRecursiveLinearSearch(n-1) + Θ(1).

Therefore,
TafRecursiveLinearSearch(n) = Θ(1) + TafRecursiveLinearSearch(n-1)

= Θ(1) + Θ(1) + TafRecursiveLinearSearch(n-2) = Θ(1) + Θ(1) + … + TafRecursiveLinearSearch(0)

= (n-1) Θ(1) = Θ(n)

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 3


Heriot-Watt University

Iterative Binary Search Asymptotic Analysis

public static long afBinarySearch(Book[] arr, long key){


int lower = 0; int upper = arr.length - 1;

while (lower <= upper){
int mid = (lower + upper) / 2;
//split the array in half

if (key == arr[mid].getYear()) { //if found


return mid; //return its index
} else if (key > arr[mid].getYear()) {
lower = mid + 1;
} else {
upper = mid - 1;
}
}
return -1; //item not found
}

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.

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 4


Heriot-Watt University

Recursive Binary Search Asymptotic Analysis

public static long afRecursiveBinarySearch( Book[] arr, int startIndex,


int endIndex, long key){
int mid = (startIndex + endIndex) / 2;

if (startIndex > endIndex) {// array is empty
return - 1; // item not found
} else if(arr[mid].getYear() == key){
return mid; // item found - return its index
} else if(arr[mid].getYear() < key){
return afRecursiveBinarySearch(arr, mid + 1, endIndex, key);
} else { // arr[mid] > key
return afRecursiveBinarySearch(arr, startIndex, mid - 1, key);
}
}

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

When n / 2k = 1 -> n = 2k -> k = log2 n, we have:

T(n) = T(1) + b log2 n


= a + b log2 n

Therefore, Recursive Binary Search is Θ(log n)

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 5


Heriot-Watt University

Appendix A - Book Class

//Encapsulated Book Class used to store the book information


public class Book {

String title;
long year;
String ISBN;
String author;

public Book(String title, long year, String ISBN, String author){
title=this.title;
this.year=year;
ISBN=this.ISBN;
author=this.author;
}

public String getTitle(){
return title;
}

public long getYear(){
return year;
}

public String getISBN(){
return ISBN;
}

public String getAuthor(){
return author;
}

public String toString(){
return "Title: " + title + " | Year: " + year + " | ISBN: " +
ISBN + " | Author: " + author;
}

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 6


Heriot-Watt University

Appendix B - bookCsw Class (driver)

public class booksCsw {

/**
* 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));
}

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 7


Heriot-Watt University

public static long afLinearSearch(Book[] Array, long key){


for(int j=0;j<Array.length;j++){
if(key == Array[j].getYear()){
return j; // item found - return its index in the
array
}
}
return -1; //item not found
}

public static long afRecursiveLinearSearch(Book[] arr, long key, int
startIndex,int endIndex) {
if (startIndex>= arr.length) { // reach the end
return -1;
} else if (key == arr[startIndex].getYear()){
return startIndex; //item found - return its index
} else
//item not found - recursively call this method in-
creasing the startIndex
return afRecursiveLinearSearch(arr, key, startIndex
+ 1, endIndex);
}

public static long afBinarySearch(Book[] arr, long key){
int lower = 0; int upper = arr.length - 1;

while (lower <= upper){
int mid = (lower + upper) / 2; //split the array in
half
if (key == arr[mid].getYear()) { //if found
return mid; //return its index
} else if (key > arr[mid].getYear()) {
lower = mid + 1;
} else {
upper = mid - 1;
}
}
return -1; //item not found
}

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 8


Heriot-Watt University

public static long afRecursiveBinarySearch( Book[] arr, int startIndex,


int endIndex, long key){
int mid = (startIndex + endIndex) / 2;

if (startIndex > endIndex) {// array is empty
return - 1; // item not found
} else if(arr[mid].getYear() == key){
return mid; // item found - return its index
} else if(arr[mid].getYear() < key){
return afRecursiveBinarySearch(arr, mid + 1, endIn-
dex, key);
} else { // arr[mid] > key
return afRecursiveBinarySearch(arr, startIn-
dex, mid - 1, key);
}
}

Data Structures & Algorithms - Coursework Part 1 - Alessio Failla - af128@hw.ac.uk 9

You might also like