Algorithm Design Methods: Spring 2007
Algorithm Design Methods: Spring 2007
Spring 2007
CSE, POSTECH
Algorithm Design Methods
Greedy method
Divide and conquer
Dynamic programming
Backtracking
Branch and bound
Some Methods Not Covered
Linear Programming
Integer programming
Simulated annealing
Neural networks
Genetic algorithms
Tabu search
Optimization Problem
A problem in which some function
(called the optimization/objective function)
is to be optimized (usually minimized or
maximized)
0~2 3~6
n/2 n/2
1 1 1 1 1 1 1 1
Quick Sort
Quicksort can be seen as a variation of mergesort
in which front and back are defined in a different
way.
Quicksort Algorithm
Partition anArray into two non-empty parts.
– Pick any value in the array, pivot.
– small = the elements in anArray < pivot
– large = the elements in anArray > pivot
– Place pivot in either part,
so as to make sure neither part is empty.
Sort small and large by recursively calling QuickSort.
How would you merge the two arrays?
– You could use merge to combine them, but because the
elements in small are smaller than elements in large, si
mply concatenate small and large, and put the result int
o anArray.
Quicksort: Complexity Analysis
Like mergesort,
a single invocation of quicksort on an array of size p
has complexity O(p):
– p comparisons = 2*p accesses
– 2*p moves (copying) = 4*p accesses
1 n-1
1 n-2
1
Quicksort: Complexity Analysis
Worst case:
There are n-1 invocations of quicksort (not counting
base cases) with arrays of size:
p = n, n-1, n-2, …, 2