Professional Documents
Culture Documents
and Algorithms I
Sorting and Searching
Dr. Tim Margush
University of Akron
2008
Goals
Be able to describe both sorting and
searching problems
Know the strategies and relative efficiencies
of the basic sorting and searching
algorithms
Be able to write methods implementing the
basic sorting and searching algorithms
2008 Dr. Tim Marg
Simple Sorting
Sorting Lists
Ordinal collection
First element, second element,
Array, ArrayList, List
Bubble Sort
Strategy
Compare adjacent elements and swap if out of
order
Do this while passing sequentially through the
list
Make additional passes until nothing more is
swapped
if (a[i].compareTo(a[i+1])>0) //swap
if (c.compareTo(a[i],a[i+1])>0) //swap
Selection Sort
Strategy
Sequentially scan unsorted portion of list for
smallest element
Swap it with first in unsorted portion
Repeat while there is more than one unsorted
item
Insertion Sort
Strategy
Pick up next unsorted element
Scan sorted section to find the correct insertion
point for the new item
Shift elements over to make room and drop new
element into the correct location
QuickSort
Strategy
Pick a "pivot" element
Hopefully the median of the list
Rearrange list so
smaller items are before the pivot (left sub list)
larger items are after the pivot (right sub list)
Sample Partition
median_loc
5
17
13
18
12
14
16
16
17
Sorting Summary
Average
Comparisons
O(n2)
Bubble
Insertion
Selection
O(n lg n)
Quicksort
Average Data
Moves
O(n2)
Bubble
Insertion
O(n lg n)
Quicksort
O(n)
Selection
2008 Dr. Tim Marg
Searching
Simple searching is based on equality
For objects: .equals
For primitives: ==
Sequential Search
Strategy
Scan list once, comparing for equality
Stop if the item is found, or every item has been
compared
Number of comparisons is O(n)
Sequential Search
static int ss(int [] a, int target){
int pos = 0;
while(
if (
return
else
return
)
)
}
2008 Dr. Tim Marg
Sequential Search
//Alternate version
static int ss(int [] a, int target){
for (int pos = 0; pos < a.length; pos++)
if (a[pos] == target) return pos;
return -1;
}
Binary Search
Precondition
The list must be sorted
Strategy
Examine the middle element and eliminate half
of the list with each comparison
Binary Search
static int bins(int [] a, int target){
int first=0, last=a.length-1, middle;
boolean found=false;
while(!found && first<=last){
middle =
if (target<a[middle])
else if (target>a[middle])
else
}
return
}
2008 Dr. Tim Marg
Binary Search
static int bins(Comparable<Shape>[] a, Shape target){
if (
)
else if (
else
Analysis of Algorithms
Experimental Analysis
Analytical Analysis
Depends on algorithm only
2008 Dr. Tim Marg
Vocabulary
Computational problem
Problem to be solved using an algorithm
Input -> Algorithm -> Output
Problem instance
One particular input set
Size of instance
Memory needed to hold input set
Basic steps
Execution time is independent of the size of the
instance
2008 Dr. Tim Marg
Example
Best case
An input resulting in fewest basic steps
Average case
Sum of basic steps over all possible instances divided
by number of instances
Expected number of steps based on expected distribution of
instances
Big O
Asymptotic Complexity
Trend as n gets large
4n3 3n2 + 6n +100
Dominant term is 4n3
Definition of O(g(n))
Restrict our discussion to positive valued
functions with domains the set of Natural
numbers
Definition: f(n) is in O(g(n)) iff there exists
a positive constant K such that f(n)/g(n) < K
for all n 1
O(g(n)) is a set of functions
We usually write O(g(n)) with a very simple g
O(n2), not O(500n2 37n +51238 + 16 lg n)
2008 Dr. Tim Marg
O(1) Constant
O(lg n) Logarithmic
O(n) Linear
O(n log n) "En-log-en"
O(n2) Quadratic
O(nk) Polynomial
O(2n) Exponential or Geometric
O(n!) Factorial or Combinatorial
2008 Dr. Tim Marg
Summary
Searching and Sorting are common
problems that have a variety of standard
algorithmic solutions
Each algorithm has an associated effciency
Some algorithms are better than others in
different circumstances
Big O classification is a way to compare
competing algorithms
2008 Dr. Tim Marg