Title: Quicksort
1(No Transcript)
2Decision Problems
Optimization problems minimum, maximum,
smallest, largest
Traveling salesman, Clique, Vertex-Cover,
Independent Set, Knapsack
Satisfaction (SAT) problems
CNF, Constraint-SAT, Hamiltonian Circuit,
Circuit-SAT
- Only optimization problems use the minimum,
maximum, smallest, largest value - To formulate the decision problem
- As input parameter for Phase II of the non
deterministic algorithms
3Computing an
Reduce and Conquer
Derek Drakes compute(a, n) if (n0)
then return 1 m ? floor(n/2) s ?
compute(a,m) if ( n is even) then return
s s if ( n is odd) then return a s
s
Divide and Conquer compute(a, n) if (n0)
then return 1 m ? floor(n/2) return
compute(a,m) compute(a, n m)
Brute Force compute(a, n) ans ? 1
for i ? 1 to n do ans ? ans a return
ans
Complexity O(n)
T(n) 2T(n/2) 1 T(1) 1 Complexity O(n)
T(n) T(n/2) 3 T(1) 1 Complexity O(log2n)
4Solving Recurrence Relations
T(n) 4T(n/2) n T(1) 1
T(n) 4T(n/2) n2 T(1) 1
4k kn2 with n 2k
4k 2k-1n 2n n with n 2k
O(n2 log2n)
O(n2 )
5Merge Sort is Stable
- We split A into two parts B (first half) and C
(first half) - Sort B and C separately
- When merging B and C, we compare elements in B
against elements in C, if tides occur, we insert
element of B back into A first and then elements
in B
6Quicksort
7Quicksort
- An element of the array is chosen. We call it the
pivot element. - The array is rearranged such that
- - all the elements smaller than the
pivot are moved before it - - all the elements larger than the
pivot are moved after it - Then Quicksort is called recursively for these
two parts.
8Quicksort - Algorithm
- Quicksort (AL..R)
- if L lt R then
- pivot Partition( AL..R)
- Quicksort (A1..L-1)
- Quicksort (AL1...R)
-
9Partition Algorithm
- Partition (AL..R)
- p ? AL i ? L j ? R 1
-
- while (i lt j) do
- repeat i ? i 1 until Ai p
- repeat j ? j - 1 until Aj p
- if (i lt j) then swap(Ai, Aj)
-
- swap(Aj,AL)
- return j
10Example
A 3 8 7 1 5 2 6 4
11Complexity Analysis
- Best Case every partition splits half of the
array - Worst Case one array is empty one has all
elements - Average case
O(n log2n)
O(n2 )
O(n log2n)
12Binary Search
BinarySearch(A1..n, el) Input A1..n sorted
in ascending order Output The position i such
that Ai el or -1 if el is not
in A1..n