Transcript L3

Intro. to Searching
Linear Search
Often, we have to search for items in a list. Linear search is the most
basic form of searching technique we have. Essentially, this is how the
algorithm works:
Start at the beginning of the list, keep looking at the list one by one
until you find what you’re looking for.
Linear Search
public int LinearSearch(int [ ] array, int key)
{
for(int k = 0; k < array.length; k++)
{
if(array[k] == key)
return k
// return the location if found
}
return -1; // return -1 if not found
}
Linear Search Algorithm Analysis
How good is it? You must search in a linear fashion. Let’s say you have n
elements.
Best case: 1 search (it’s the first element you search.)
Average case: n/2
Worst case: n or order of n.
Linear search has a runtime complexity of an order of n. Another way of
saying this is f(n) = n;
Binary Search
Before we can do binary search, we must have an ordered list.
You cannot perform binary search on an unordered list. This list may
be in ascending or descending order.
Binary Search relies on repeatedly cutting the list in equal halves and
then analyzing each half separately. It keeps doing this procedure
Binary Search Demo
public int binarysearch(int[ ] array, int key)
{
int left;
int right = array.length – 1;
while(left <= right)
{
int midpoint = (left + right) / 2;
if(array[midpoint] == key)
return midpoint;
else if(array[midpoint] < key)
left = midpoint + 1;
else
right = midpoint -1;
}
return -1;
}
Binary Search Algorithm Analysis
How good is it? Let’s say you have n elements.
Best case: 1 search (it’s the first element you search.)
Average case: log(n)/2
Worst case: log(n)
Linear search has a runtime complexity of an order of n. Another way of
saying this is f(n) = log(n);
Linear Search
Binary Search
f(n) = n
f(n) = log(n)
f(64) = 64
f(65,536) = 65536
f(1,048,576) = 1,048,576
f(1,073,741,824) = 1,073,741,824
f(64) = 6
f(65536) = 16
f(1,048,576) = 20
f(1,073,741,824) = 30
Conclusion
Binary search is significantly faster than linear search. It is worth it to
get our lists in order so that when we need to find things.
Exercise 1
Create two arrays.
Array1 is to be an integer array of size 100 with random values between
1-100.
Array2 is to be an integer array of size 100 with random values between
1-10000 but build this array so that it is already sorted.
Array2[i] = (int)(Math.random()*X + X);
Let X = 5 initially and take X up by 5 every iteration of the loop.
Exercise 2: Use Recursion
public int LinearSearch(int [ ] array, int key)
{
for(int k = 0; k < array.length; k++)
{
if(array[k] == key)
return k
// return the location if found
}
return -1; // return -1 if not found
}
Build the following code above using recursion. Run linear search on Array 1.
Exercise 3: Write a recursive solution to
binary search, use it on Array2.
public int binarysearch(int[ ] array, int key)
{
int left;
int right = array.length – 1;
while(left <= right)
{
int midpoint = (left + right) / 2;
if(array[midpoint] == key)
return midpoint;
else if(array[midpoint] < key)
left = midpoint + 1;
else
right = midpoint -1;
}
return -1;
}