Download presentation
Presentation is loading. Please wait.
Published byRichard Allen Modified over 9 years ago
1
Foundations of Data Structures Practical Session #12 Linear Sorting
2
Sorting algorithms criteria 2
3
Comparison sorting 3
4
Linear sorting numeric Linear sorting is the problem of sorting a collection of items with numeric keys. The ability to perform arithmetic operations on the keys allows faster algorithms than comparison-based algorithms in many cases. Classical examples include: Counting sort Radix sort Bucket sort 4
5
Counting sort 5
6
Counting sort cont’d Counting-Sort (A, B, k) for i ← 1 to k C[i] ← 0 // Calc histogram for j ← 1 to n C[A[j]] ← C[A[j]] + 1 // Calc start index (backwards) in output for each key for i ← 2 to k C[i] ← C[i] + C[i-1] // Copy to output array for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 return B Example A:323131 6 C:2 1 3 2 3 6 B:112333 C:0 0 0
7
Counting sort analysis 7
8
Radix sort 8
9
Radix sort analysis For example, sort 7 numbers with 3 digits in decimal base. 9 329457657839436720355 720355436457657329839 720329436839355457657 329355436457657720839 Sorted by 1 st digit Sorted by 2 nd (and 1 st ) digit Sorted! Input array
10
Radix sort cont’d 10
11
Bucket sort 11
12
Bucket sort cont’d Example 12 A:0.50.10.70.2.15.27 B: 0.1.15 0.2.27 0.50.7 A:0.1.15.2.270.50.7
13
Bucket sort analysis 13
14
Sort algorithms review Stable In Place Extra Space Running time KeysType Worst case Average √√O(1)O(n 2 )any Insertion sort √XO(n)O(nlogn)any Merge sort X√O(1)O(nlogn)any Heap sort X√O(1)O(n 2 )O(nlogn)anyQuicksort √XO(n+k) integers [1..k] Counting sort √ Depends on the stable sort used O(d(b+n)) d digits in base b Radix sort √XO(n)O(n 2 )O(n)[0,1) Bucket sort 14
15
Question 1 15
16
Question 1 solution 16
17
Question 2 17
18
Question 2 solution 18
19
Question 2 solution cont’d 19
20
Question 2 solution cont’d 20
21
Question 3 21
22
Question 3 solution 22
23
Question 4 23
24
Question 4 solution 24
25
Question 4 solution cont’d 25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.