More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, …...
-
Upload
norman-ross -
Category
Documents
-
view
250 -
download
0
Transcript of Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, …...
quick sort, lower bound on sorting, bucket sort,
radix sort, comparison of algorithms, code, …
Sorting: part 2
sortingquick sort
Demo with cards?
1. Divide S into 3 parts as follows• Select an element e• Create L with all element is S less than e• Create E with all element in S equal to e• Create G with all element in S greater than e
2. Recur by quick sorting L and quick sorting G3. Conquer by appending L to E to G
Divide & Conquer (with quick sort)
sortingquick sort
Demo with cards?
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
Stopping condition
sortingquick sort
Less than, Equal to, Greater than
sortingquick sort
pivot
sortingquick sort
Get the data sets L, E, and G
sortingquick sort
Recurse & conquer
sortingquick sort
sortingquick sort
.. and this happens if S is sorted!
sortingquick sort
sortingquick sort
Two enhancements• improve selection of pivot
• randomise• “in place” sorting
• uses no more space and is fast
sorting
A lower bound on comparison based sorting
i.e. how good can it get?
sortingA lower bound on comparison based sorting
• Assume we are given a sequence S, of length n, to be sorted (no duplicates)• an algorithm compares pairs of elements with two outcomes
• x < y TRUE• x < y FALSE
• we can represent a comparison based sorting algorithm as a decision tree• i.e. represent its behaviour• internal node represents a decision• left is TRUE, right is FALSE
• there are n! possible orderings of S• there must be n! leaves in our decision tree
• one for each possible ordering of S• running time of algorithm must be at least the height of the decision tree• the height of the decision tree is at least log(n!) base 2• log(n!) is O(n.log(n))
sorting
stable sorting
sortingstable sorting
A sorting algorithm is stable if for any two entries x and yof S such that
• x.getKey() == y.getKey() and • x precedes y in S before sorting• x precedes y after S is sorted
sorting
bucket sortaka
pigeonhole sort
sorting
bucket sortaka
pigeonhole sort
PIGEONHOLE SORT
sortingbucket sort (aka pigeonhole sort)
Use the sey of an object as an index into a bucket array
Assume keys are in range [0,N-1]
Assume we have n object to sort
insert object e into bucket[e.getKey()]
Complexity is O(n+N)
sortingbucket sort (aka pigeonhole sort)
Use the sey of an object as an index into a bucket array
Assume keys are in range [0,N-1]
Assume we have n object to sort
insert object e into bucket[e.getKey()]
Complexity is O(n+N)
How might we do this in java?What data structures would we use?
Is it stable?What kind of data sets would be suitable?
sorting
radix sort
We will describe by example
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89
28
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89
28
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89
28
81
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89
28
81
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69
28
81
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69
28
81
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69
28
81
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69
28
81
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69
28
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69
28
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29
28
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29
28
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29
28, 18
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29
28, 18
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29,39
28, 18
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29,39
28, 18
81, 31
14
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29,39
28, 18
81, 31
14
17
Sort on least significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
89, 69, 29,39
28, 18
81, 31
14
17
Sorted on least significant digit!
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
14
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
14
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
14, 17
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
14, 17
28
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
14, 17
28
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
14, 17, 18
28
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81
31
14, 17, 18
28
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81, 89
31
14, 17, 18
28
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81, 89
31
14, 17, 18
28
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81, 89
31
14, 17, 18
28
69
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81, 89
31
14, 17, 18
28
69
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81, 89
31
14, 17, 18
28, 29
69
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81, 89
31
14, 17, 18
28, 29
69
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sort on next (second) significant digit
81, 89
31, 39
14, 17, 18
28, 29
69
sortingradix sort
5
6
7
8
9
0
1
2
3
4
Sorted on next (second) significant digit!
81, 89
31, 39
14, 17, 18
28, 29
69
sortingradix sort
• We can extend this to numbers of any length• We can do this on strings
Considering numbers to base b• require b buckets• require d = log(m) scans (d is number of digits)
• log is to base b• m is largest number to be sorted
• each scan is of order n• therefore O(d.n)
sorting
comparison of algorithms
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sortingcomparison of algorithms
Bubble sort
Insertion sort
Selection sort
Merge sort
Quick sort
Heap sort
Bucket sort
Radix sort
sorting
Yes, but how did you sort out your CD’s?
not covered sorting
Sift sortShell sortBead sort
not covered sorting
Sift sortShell sortBead sort
not covered sorting
Sift sortShell sortBead sort
not covered sorting