You are on page 1of 40

Data Structures

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

Simple sorting is based on comparisons


Comparable / Comparator
elt_a.compartTo(elt_b)
cptr.compare(elt_a, elt_b)

Primitives are compared using <, ==, and >


2008 Dr. Tim Marg

Sorting Requires Effort


Comparisons take time
s1.compareTo(s2)

Moving things in lists takes time


temp = theList.get(k)
theList.set(k, theList.get(j))
theList.set(j, temp)

Compare sorting efficiency by counting


comparisons or data movements
2008 Dr. Tim Marg

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

2008 Dr. Tim Marg

Bubble Sort's Innermost task


static void bs(int [] a){

if (a[i] > a[i+1]) //swap

2008 Dr. Tim Marg

Bubble Sort's Innermost task


static void bs(Comparable [] a){

if (a[i].compareTo(a[i+1])>0) //swap

2008 Dr. Tim Marg

Bubble Sort's Innermost task


static void bs(Whatever [] a, Comparator c){

if (c.compareTo(a[i],a[i+1])>0) //swap

2008 Dr. Tim Marg

Bubble Sort Analysis


for(int maxElt=a.length-1; maxElt>0; maxElt--)
for(int i=0; i<maxElt; i++)
if (a[i] > a[i+1]){
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}

2008 Dr. Tim Marg

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

2008 Dr. Tim Marg

Selection Sort's Innermost task


static void ss(int [] a){

if (a[i] < a[smallest]) //new smallest

2008 Dr. Tim Marg

Selection Sort Analysis


for(int start=0; start<a.length-1; start++){
int smallest = start;
for(int scan=start+1; scan<a.length; scan++)
if (a[scan] < a[smallest])
smallest = scan;
temp = a[smallest];
a[smallest] = a[start];
a[start] = temp;
}
2008 Dr. Tim Marg

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

2008 Dr. Tim Marg

Insertion Sort's Innermost task


static void is(int [] a){

while(a[inloc-1] > nextitem) //slide

2008 Dr. Tim Marg

Insertion Sort Analysis


for(int pu=1; pu<a.length; pu++){
int nextitem = a[pu];
int inloc = pu;
while(inloc>0 && a[inloc-1]>nextitem){
a[inloc] = a[inloc-1];
inloc--;
}
a[inloc] = nextitem;
}
2008 Dr. Tim Marg

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)

Sort the left and right sub lists


May use recursion

2008 Dr. Tim Marg

Quick Sort's Innermost task


static void qs(int [] a, int start, int stop){
int ploc = partition(a, start, stop)
qs(a, start, ploc-1);
qs(a, ploc+1, stop)

2008 Dr. Tim Marg

Quick Sort Analysis


int partition(int[] a, int start, int stop) {
swap(a, start, median_loc); //arbitrary element
pvt is at start position
int pvtloc = start;
elts after start through
for(int scan=start+1; scan<=stop; scan++)
pvtloc are < pivot
if (a[scan] < a[start]){ //small number
elts after pvtloc and
before scan are >= pvt
pvtloc++;
elts at scan and to right
swap(a, pvtloc, scan);
are untested
}
swap(a, start, pvtloc);
return pvtloc;
}
2008 Dr. Tim Marg

Sample Partition

median_loc
5

17

13

18

12

14

16

16

2008 Dr. Tim Marg

17

Quick Sort Analysis


static void qs(int [] a, int start, int stop){
if (start<stop){
int ploc = partition(a, start, stop)
qs(a, start, ploc-1);
qs(a, ploc+1, stop)
}
}
2008 Dr. Tim Marg

Quick Sort Public


static void qs(int [] a){
qs(a, 0, a.length-1);
}

2008 Dr. Tim Marg

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: ==

Advanced searching may rely on additional


properties, such as
Comparable / Comparator
<>
Others?
2008 Dr. Tim Marg

Searching Requires Effort


Comparisons take time
s1.equals(s2)

Compare searching efficiency by counting


comparisons

2008 Dr. Tim Marg

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)

2008 Dr. Tim Marg

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;
}

2008 Dr. Tim Marg

Binary Search
Precondition
The list must be sorted

Strategy
Examine the middle element and eliminate half
of the list with each comparison

Number of comparisons is O(lg n)

2008 Dr. Tim Marg

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

2008 Dr. Tim Marg

Recursive Binary Search


static int bins(int [] a, int target, int first, int last){
if (first >= last) return
int middle =
if (target<a[middle])
return
else if (target>a[middle])
return
else
return
}
2008 Dr. Tim Marg

Analysis of Algorithms
Experimental Analysis

Depends on efficiency of the program


Depends on compiler
Depends on operating system
Depends on hardware
Depends on test data

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

Problem: Find the largest int in a collection


Input: An array of ints of size n
Output: the largest int in tha array
Algorithm

2008 Dr. Tim Marg

Counting Basic Steps


//Find an element smaller than target
for (int k = 0; k < n; k++){
if (a[k] < target) return k;
}
return -1;

2008 Dr. Tim Marg

Worst, Best, Average


Worst case

For an instance of a particular size

An input that causes the most basic steps


Maximum steps required over all instance (of this size)

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

2008 Dr. Tim Marg

Big O
Asymptotic Complexity
Trend as n gets large
4n3 3n2 + 6n +100
Dominant term is 4n3

Ignore constants and constant multiples


4n3, -100n3, 200000n3, n3-500, 7(4n3+1000)
All O(n3)

2008 Dr. Tim Marg

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

Common Complexity Classes

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

You might also like