Академический Документы
Профессиональный Документы
Культура Документы
Lecture Outline
Iterative
Recursive
Divide-and-Conquer
Best/Worst/Average-case bounds
Randomized Algorithms
3
--64
Sorting Applications
Uniqueness testing
Deleting duplicates
Prioritizing events
Frequency counting
Reconstructing the original order
Set intersection/union
Finding a target pair x, y such that x+y = z
Efficient searching
4
--64
Selection Sort
1.
2.
3.
6
--64
10
14
37
13
29
10
14
13
37
13
10
14
29
37
13
10
14
29
37
10
13
14
29
37
Unsorted
items
Sorted items
7
--74
Step 1.
Searching for
Maximum element
Step 2.
Swap maximum
element with the
last item
29
10
14
37
13
8
--64
Number of times
executed
n-1
n-1
(n-1)+(n-2)++1
= n(n-1)/2
n-1
Total
= c1(n-1) +
c2*n*(n-1)/2
c1 and c2 = cost of stmts in outer and inner block
= O(n2)
Data movement is minimal
9
--64
Bubble Sort
1.
2.
3.
4.
Analogy:
x
x
Sorted Item
Pair of items
under comparison
12
--64
29
10
14
37
Step 1.
Compare
adjacent pairs
of numbers
Step 2.
Swap if the
items are out
of order
13
13
--64
14
--64
11 25 39
Idea:
Worst-case
Best-case
17
--64
Insertion Sort
10
2nd card: 5
5
10
3rd card: K
10
19
--64
40 13 20
Iteration 1
13 40 20
Iteration 2
13 20 40
Iteration 3
Sorted
Unsorted
Unsorted
To be inserted
13 20 40
20
--64
next : the
item to be
inserted
Shift sorted
items to make
place for next
a[j+1] = next;
Insert next
to the correct
location
}
}
29
10
14
37
13
21
--64
Merge Sort
Question:
Divide Step:
Conquer Step:
25
--64
Conquer Step:
26
-64
Divide into
two halves
Recursively sort
the halves
Merge them
Question:
Note:
low >= high is the base case, i.e the array has 0 or 1 item
28
--64
38 16 27 39 12 27
38 16 27
38 16
38
39 12 27
27
16
39 12
Divide Phase
39
16 38
27
12
12 39
Recursive call to
mergeSort()
Conquer Phase
Merge steps
16 27 38
12 27 39
12 16 27 27 38 39
29
--64
a[0..2]
a[3..5]
2 4 5
3 7 8
2 4 5
3 7 8
2 4 5
3 7 8
2 3
2 4 5
3 7 8
2 3 4
24 5
3 7 8
2 3 4 5
Unmerged
items
2 4 5
3 7 8
2 3 4 5 7 8
Merged items
Merged result in a
temporary array
30
--64
Normal Merging
Where both
halves have
unmerged items
31
--64
Merged result
are copied
back into a[]
delete [] b;
Remember to free
allocated memory
Question:
In mergeSort()
Level 1:
mergeSort n/2 items
Level 2:
mergeSort n/22 items
n/2
n/22
n/22
n = 2k
Level 2:
22 calls to mergeSort
n/(2k) = 1
n/22
1 1
Level 1:
2 calls to mergeSort
n/2
n/22
Level 0:
1 call to mergeSort
1 1
k = lg n
34
--64
Level (lg n): 2lg n-1(= n/2) calls to merge() with n/2lgn (=
1) item in each half, O(n) time
Total time complexity = O(n lg(n))
Optimal comparison based sort method
35
--64
Pros:
Cons:
Quick Sort
Choose first
element as pivot
Partition a[] about
the pivot 27
Recursively sort
the two parts
27 38 12 39 27 19
16
Pivot
12 16
27
39 27 38
Pivot
12 16 27 27 38 39
39
--64
Partition
a[low..high]
and return the index
of the pivot item
Recursively sort
the two portions
}
}
S1 = a[i+1...m]
: items < p
S2 = a[m+1...k-1]: items p
Unknown = a[k...j] : items to be assigned
to S1 or S2
p
<p
m
i
S1
?
k
S2
Unknown
41
--64
p
i
?
j
k
Unknown
42
--64
if y p
<p
m
S2
<p
i
S1
y
k
S1
Increment k
Unknown
y
k
S2
Unknown
43
--64
If y < p
<p
S1
Increment m
<p
<p
S1
Increment k
<p
i
S1
S2
Unknown
S2
S1
Swap x & y
S2
k
S2
j
44
--64
p is the pivot
S1 and S2 empty
initially
Go through each
element in unknown
region
Case 1 :
do nothing!
Swap pivot with a[m]
m is the index of pivot
45
--64
46
--64
N = high - low
Complexity is O(N)
47
--64
p = a[i]
S2 = a[m+1, j]
S1 = a[i+1, m]
empty when m = i
n-1
1
Total no.
of levels
=n
n-2
Depth of recursion is lg n
50
--64
It is known that:
51
--64
Radix Sort
53
--64
10 groups. Each is a
queue to retain the
order of item
55
--64
Question:
Basic Idea:
Properties of Sorting
In-Place Sorting
Question:
60
--64
Stable Sorting
Example:
A stable sorting algorithm will make all within the same group
to appear in alphabetical order
61
--64
Selection Sort:
1285 5a 4746 602 5b (8356)
1285 5a 5b 602 (4746 8356)
602 5a 5b (1285 4746 8356)
5b 5a (602 1285 4746 8356)
Quick Sort:
1285 5a 150 4746 602 5b 8356 (pivot = 1285)
1285 (5a 150 602 5b) (4746 8356)
5b 5a 150 602 1285 4746 8356
62
--64
Best Case
In-place?
Stable?
Selection
Sort
O(n2)
O(n2)
Yes
No
Insertion
Sort
O(n2)
O(n)
Yes
Yes
Bubble Sort
O(n2)
O(n2)
Yes
Yes
Bubble Sort
2
O(n2)
O(n)
Yes
Yes
Merge Sort
O(n lg n)
O(n lg n)
No
Yes
Quick Sort
O(n2)
O(n lg n)
Yes
No
Radix sort
O(dn)
O(dn)
No
yes
63
--64
Summary
Iterative Sorting:
Recursive Sorting:
Merge Sort
Quick Sort
Selection Sort
Bubble Sort
Insertion Sort
Radix Sort
In-Place
Stable
64
--64