Professional Documents
Culture Documents
divide
10/07/16 02:54 AM
http://www.nurul.com
10/07/16 02:54 AM
http://www.nurul.com
Recursive algorithm
10/07/16 02:54 AM
http://www.nurul.com
Start Sort(L)
If L has length greater than 1 then
Begin
End
End Algorithm
10/07/16 02:54 AM
http://www.nurul.com
Analyzing Divide-and-Conquer
Algorithms
When an algorithm contains a recursive call to
itself, its running time can often be described by
a recurrence equation which describes the overa
ll running time on a problem of size n in terms of
the running time on smaller inputs.
For divide-and-conquer algorithms, we get
recurrences that look like:
(1)
T(n) =
if n < c
aT(n/b) +D(n) +C(n) otherwise
10/07/16 02:54 AM
http://www.nurul.com
http://www.nurul.com
10/07/16 02:54 AM
http://www.nurul.com
T(n)
2T(n/2) n)
otherwise
a = 2(two subproblems)
n/b = n/2 (each subproblem has size approx n/2)
D(n) = (1) (just compute midpoint of array)
C(n) =n) (merging can be done by scanning sorted
subarrays)
10/07/16 02:54 AM
http://www.nurul.com
10/07/16 02:54 AM
http://www.nurul.com
Algorithm
http://www.nurul.com
10
Algorithm (cont.)
10/07/16 02:54 AM
http://www.nurul.com
11
Pseudocode
Procedure MAXMIN(S):
/* This procedure returns the max and min elements of
S. */
case S = {a } : return (a, a)
begin
case S {a, b} : return (MAX(a, b), MIN(a, b))
else : divide S into two subsets S1 and S2, each with
half the elements;
(max1, min1) MAXMIN(S1);
(max2, min2) MAXMIN(S2);
return (MAX(max1, max2), MIN(min1, min2))
10/07/16 02:54 AM
http://www.nurul.com
12
Merge Sort
How do we partition?
10/07/16 02:54 AM
http://www.nurul.com
13
Partitioning
Choice
1
First n-1 elements into set A, last element set B
Sort A using this partitioning scheme recursively
B already sorted
ci
i 2
10/07/16 02:54 AM
n(n 1)
2
http://www.nurul.com
14
Partitioning - Choice 2
All O(n2)
10/07/16 02:54 AM
http://www.nurul.com
15
Partitioning - Choice 3
10/07/16 02:54 AM
http://www.nurul.com
16
Merge Sort
Idea:
Take the array you would like to sort and divide
it in half to create 2 unsorted subarrays.
Next, sort each of the 2 subarrays.
Finally, merge the 2 sorted subarrays into 1
sorted array.
Efficiency: O(nlog2n)
10/07/16 02:54 AM
http://www.nurul.com
17
Merge Sort
10/07/16 02:54 AM
http://www.nurul.com
18
Merge Sort
Although the merge step produces a sorted
array, we have overlooked a very important
step.
How did we sort the 2 halves before
performing the merge step?
We used merge sort!
10/07/16 02:54 AM
http://www.nurul.com
19
Merge Sort
By continually calling the merge sort
algorithm, we eventually get a subarray of
size 1.
Since an array with only 1 element is
clearly sorted, we can back out and merge 2
arrays of size 1.
10/07/16 02:54 AM
http://www.nurul.com
20
Merge Sort
10/07/16 02:54 AM
http://www.nurul.com
21
Merge Sort
The basic merging algorithm consists of:
2 input arrays (arrayA and arrayB)
An ouput array (arrayC)
3 position holders (indexA, indexB, indexC),
which are initially set to the beginning of their
respective arrays.
10/07/16 02:54 AM
http://www.nurul.com
22
Merge Sort
The smaller of arrayA[indexA] and
arrayB[indexB] is copied into
arrayC[indexC] and the appropriate position
holders are advanced.
When either input list is exhausted, the
remainder of the other list is copied into
arrayC.
10/07/16 02:54 AM
http://www.nurul.com
23
Merge Sort
arrayA
13
24
26
15
27
38
indexA
arrayB
2
indexB
arrayC
We compare arrayA[indexA]
with arrayB[indexB].
Whichever value is smaller
placed into arrayC[indexC].
1 < 2 so we insert
arrayA[indexA] into
arrayC[indexC]
indexC
10/07/16 02:54 AM
http://www.nurul.com
24
Merge Sort
arrayA 1
13
24
26
27
38
indexA
2
arrayB
15
2 < 13 so we insert
arrayB[indexB] into
arrayC[indexC]
indexB
1
arrayC
indexC
10/07/16 02:54 AM
http://www.nurul.com
25
Merge Sort
arrayA 1
13
24
26
27
38
indexA
arrayB 2
15
13 < 15 so we insert
arrayA[indexA] into
arrayC[indexC]
indexB
1
arrayC
2
indexC
10/07/16 02:54 AM
http://www.nurul.com
26
Merge Sort
arrayA 1 13
24
26
indexA
arrayB 2
15
27
38
15 < 24 so we insert
arrayB[indexB] into
arrayC[indexC]
indexB
1
arrayC
13
10/07/16 02:54 AM
indexC
http://www.nurul.com
27
Merge Sort
arrayA 1 13
24
26
indexA
arrayB 2 15
27
38
24 < 27 so we insert
arrayA[indexA] into
arrayC[indexC]
indexB
1
arrayC
2 13
10/07/16 02:54 AM
15
indexC
http://www.nurul.com
28
Merge Sort
arrayA 1 13 24
26
indexA
arrayB 2 15
27
38
26 < 27 so we insert
arrayA[indexA] into
arrayC[indexC]
indexB
1
arrayC
2 13 15
10/07/16 02:54 AM
24
indexC
http://www.nurul.com
29
Merge Sort
arrayA 1 13 24 26
arrayB 2 15
27
38
indexB
arrayC
2 13 15 24
26
indexC
10/07/16 02:54 AM
http://www.nurul.com
30
Merge Sort
arrayA 1 13 24 26
arrayB 2 15 27 38
1
arrayC
2 13
10/07/16 02:54 AM
15 24 26 27 38
http://www.nurul.com
31
10/07/16 02:54 AM
http://www.nurul.com
32
10/07/16 02:54 AM
http://www.nurul.com
33
10/07/16 02:54 AM
http://www.nurul.com
35
10/07/16 02:54 AM
http://www.nurul.com
36
Evaluation
Recurrence equation:
Assume n is a power of 2
c1
if n=1
T(n) =
2T(n/2) + c2n
if
n>1, n=2k
10/07/16 02:54 AM
http://www.nurul.com
37
Solution
By Substitution:
T(n) = 2T(n/2) + c2n
T(n/2) = 2T(n/4) + c2n/2
T(n) = 4T(n/4) + 2 c2n
T(n) = 8T(n/8) + 3 c2n
T(n) = 2iT(n/2i) + ic2n
10/07/16 02:54 AM
http://www.nurul.com
38
Solution (cont.)
Assuming n = 2k, expansion halts when we
get T(1) on right side; this happens when
i=k
T(n) = 2kT(1) + kc2n
Since 2k=n, we know k=log n; since T(1) =
c1, we get
T(n) = c1n + c2nlogn; thus an upper bound
for TmergeSort(n) is O(nlogn)
Quick Sort
10/07/16 02:54 AM
http://www.nurul.com
40
Quick Sort
In the bubble sort, consecutive items are compared
and possibly exchanged on each pass through the
list.
This means that many exchanges may be needed
to move an element to its correct position.
Quick sort is more efficient than bubble sort
because a typical exchange involves elements that
are far apart, so fewer exchanges are required to
correctly position an element.
10/07/16 02:54 AM
http://www.nurul.com
41
Quick Sort
Each iteration of the quick sort selects an
element, known as the pivot, and divides the
list into 3 groups:
Elements whose keys are less than (or equal to)
the pivots key.
The pivot element
Elements whose keys are greater than (or equal
to) the pivots key.
10/07/16 02:54 AM
http://www.nurul.com
42
Quick Sort
The sorting then continues by quick
sorting the left partition followed by quick
sorting the right partition.
The basic algorithm is as follows:
10/07/16 02:54 AM
http://www.nurul.com
43
Quick Sort
Partitioning Step: Take an element in the unsorted
array and determine its final location in the sorted
array. This occurs when all values to the left of
the element in the array are less than (or equal to)
the element, and all values to the right of the
element are greater than (or equal to) the element.
We now have 1 element in its proper location and
two unsorted subarrays.
Recursive Step: Perform step 1 on each unsorted
subarray.
10/07/16 02:54 AM
http://www.nurul.com
44
Quick Sort
Each time step 1 is performed on a
subarray, another element is placed in its
final location of the sorted array, and two
unsorted subarrays are created.
When a subarray consists of one element,
that subarray is sorted.
Therefore that element is in its final
location.
10/07/16 02:54 AM
http://www.nurul.com
45
Quick Sort
There are several partitioning strategies used in
practice (i.e., several versions of quick sort), but
the one we are about to describe is known to work
well.
For simplicity we will choose the last element to
be the pivot element.
We could also chose a different pivot element and
swap it with the last element in the array.
10/07/16 02:54 AM
http://www.nurul.com
46
Quick Sort
Below is the array we would like to sort:
1
10/07/16 02:54 AM
11
http://www.nurul.com
10
47
Quick Sort
The index left starts at the first element and right
starts at the next-to-last element.
1
11
left
10
right
http://www.nurul.com
48
Quick Sort
We move left to the right, skipping over
elements that are smaller than the pivot.
11
left
10/07/16 02:54 AM
10
right
http://www.nurul.com
49
Quick Sort
We then move right to the left, skipping over
elements that are greater than the pivot.
1
8
left
11
10
right
http://www.nurul.com
50
Quick Sort
If left is to the left of right (or if left =
right), those elements are swapped.
1
11
left
1
5
left
10
10
right
9
11
8
right
Quick Sort
The effect is to push a large element to the
right and a small element to the left.
We then repeat the process until left and
right cross.
10/07/16 02:54 AM
http://www.nurul.com
52
Quick Sort
1
11
left
1
10
right
9
11
10
11
10
left right
1
left right
Quick Sort
1
11
10
11
10
left right
right left
10/07/16 02:54 AM
http://www.nurul.com
54
Quick Sort
1
11
10
right left
right left
11
10
Quick Sort
Note that all elements to the left of the pivot
are less than (or equal to) the pivot and all
elements to the right of the pivot are greater
than (or equal to) the pivot.
Hence, the pivot element has been placed in
its final sorted position.
1
right left
11
10
Quick Sort
We now repeat the process using the subarrays to the left and right of the pivot.
1
10/07/16 02:54 AM
11
10
11
10
http://www.nurul.com
57
for special
list[left]=list[right]=pivot
10/07/16 02:54 AM
http://www.nurul.com
59
Quick Sort
A couple of notes about quick sort:
There are more optimal ways to choose the
pivot value (such as the median-of-three
method).
Also, when the subarrays get small, it
becomes more efficient to use the insertion
sort as opposed to continued use of quick
sort.
10/07/16 02:54 AM
http://www.nurul.com
60
Analysis of Quicksort
10/07/16 02:54 AM
http://www.nurul.com
63
C(3) = 2 + C(2) = 2 + 1
C(4) = 3 + C(3) = 3 + 2 + 1
... ...
C(n) = n - 1 + C(n - 1)
= (n - 1) + (n - 2) + ... + 2 + 1
= (n - 1)n/2
Average Case
Analysis(cont.)
Let C(n) be average number of comparisons
(C(n)-2)/(n+1) = 2 Hn+1
(C(n)-2)/(n+1) = 2 ln n + O(1)
C(n)
=(n+1)*2 ln n + (n+1)*O(1)
= 2n ln n + O(n)