Upload
neeldhara-misra
View
82
Download
2
Embed Size (px)
Citation preview
CS 321. Algorithm Analysis & Design Lecture 1
Sorting
Announcements
Announcements
The course website is at http://cs321.xyz
Please take the informal survey on the website, or send me an email with the subject [CS321].
Agenda
Agenda
Sorting Algorithms
Agenda
Sorting Algorithms
Lower Bounds
Agenda
Sorting Algorithms
Lower Bounds
Preference Aggregation
Sorting
4 4 2 1 2 5 6
1 2 2 4 7 9 7
6 4 5 5 9 4 3
7 4 5 9 7 3 8
4 4
21 2
5 6
1 2 2
4
7 97
6
4
5 5
9
43
7
4
5
97
3
8
Counting Sort
45986 45382 39590 99921
75987 55321 78324 52011
12001 99215 54213
32007 14261 12345 77666
11100
45986 45382 39590 99921
75987 55321 78324 52011
12001 99215 54213
32007 14261 12345 77666
11100
45986 4538239590
9992175987
55321
78324
52011
12001
99215
54213
32007
14261 12345
77666
11100
459864538239590
99921
7598755321
78324
52011
12001
99215
54213
32007
1426112345
77666
11100
Bucket Sort
21 25 32 45 90 63 72
21 25 32 45 90 63 72
21 25 32 45 90 63 72
21 25 32 45 90 63 72
21 25 32 45 90 63 72
21 25 32 45 90 63 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
21 25 32 45 9063 72
Bubble Sort
21 25 32 45 63 72
35
21 25 32 45 63 72
35
21 25 32 45 63 72
35
21 25 32 45 63 72
35
21 25 32 45 63 72
35
21 25 32 45 63 7235
24 31 23 14 56 62 11
10 32 21 89 22 54 12
42 19 51 78 9 14 3
99 27 50 88 63 70 81
24 31 23 14 56 62 11
10 32 21 89 22 54 12
42 19 51 78 9 14 3
99 27 50 88 63 70 81
24 31 23 14 56 62 11
10 32 21 89 22 54 12
42 19 51 78 9 14 3
99 27 50 88 63 70 81
24 3123 14 56 62 11
10 32 21 89 22 54 12
42 19 51 78 9 14 3
99 27 50 88 63 70 81
24 3123 14 56 62 11
10 32 21 89 22 54 12
42 19 51 78 9 14 3
99 27 50 88 63 70 81
24 312314 56 62 11
10 32 21 89 22 54 12
42 19 51 78 9 14 3
99 27 50 88 63 70 81
21 25 32 45 63 72
35
21 25 32 45 63 72
35
Use binary search to reduce the number of comparisons.
Insertion Sort
21 25 32 41 55 63 72
10 22 45 53 62 64 90
21 25 32 41 55 63 72
10 22 45 53 62 64 90
21 25 32 41 55 63 72
10
22 45 53 62 64 90
21
25 32 41 55 63 72
10
22 45 53 62 64 90
21
25 32 41 55 63 72
10 22
45 53 62 64 90
21 25
32 41 55 63 72
10 22
45 53 62 64 90
21 25 32
41 55 63 72
10 22
45 53 62 64 90
21 25 32 41
55 63 72
10 22
45 53 62 64 90
21 25 32 41
55 63 72
10 22 45
53 62 64 90
21 25 32 41
55 63 72
10 22 45
53
62 64 90
21 25 32 41
55 63 72
10 22 45
53 62 64 90
Merging two sorted arrays of length n each takes O(n) comparisons.
There are log n layers of recursion.
There are log n layers of recursion.
The work done at each layer is O(n).
There are log n layers of recursion.
The work done at each layer is O(n).
Total time spent is O(n log n).
Merge Sort
Pick a “pivot”.
Pick a “pivot”.
Recurse!
“Merging” is trivial.
The recursion tree may be very skewed.
Quick Sort
Sorting AlgorithmsCounting Sort
Bubble Sort
Merge Sort
Quick Sort
Insertion Sort
O(n + k)
O(n2)
O(n log n)
O(n2)
O(n2)
Sorting AlgorithmsCounting Sort
Bubble Sort
Merge Sort
Quick Sort
Insertion SortBinary
O(n + k)
O(n2)
O(n log n)
O(n2)
O(n2)
Sorting AlgorithmsCounting Sort
Bubble Sort
Merge Sort
Quick Sort
Insertion SortBinary
Randomised
O(n + k)
O(n2)
O(n log n)
O(n2)
O(n2)
References
References
Look up “The Sound of Sorting” on Youtube.
References
Look up “The Sound of Sorting” on Youtube.
http://www.sorting-algorithms.com
Agenda
Sorting Algorithms
Lower Bounds
Preference Aggregation
Happy New Year