Counting Sort A sort algorithm that is not
- Slides: 43
Counting Sort – מיון מנייה A sort algorithm that is not based on comparisons, and supports duplicate keys. • A is an input array of length n. • B is the output array of length n. • C is an auxiliary array of size k. • Assumption: the input array A consists of elements with integer keys in the range [1. . k].
Counting Sort for i ← 1 to k C[i] ← 0 for j ← 1 to n C[A[j]] ← C[A[j]] + 1 for i ← 2 to k C[i] ← C[i] + C[i-1] for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] - 1 return B // Initialize auxiliary array C // C[i] = the number of appearances of i in A // C[i] = the number of elements in A that are ≤ i // Putting elements in their correct place in array B // Decreasing the amount of elements A[j] left
Counting Sort for i ← 1 to k C[i] ← 0 A 3 2 3 C 0 0 0 1 3 1
Counting Sort for j ← 1 to n C[A[j]] ← C[A[j]] + 1 // C[i] = the number of appearances of i in A A 3 2 3 C 2 1 3 1
Counting Sort for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i A 3 2 3 C 2 1 3 1
Counting Sort for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i A 3 2 3 C 2 1 3 i=2 1 3 1
Counting Sort for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i A 3 2 3 C 2 3 3 i=2 1 3 1
Counting Sort for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i A 3 2 3 C 2 3 3 i=3 1
Counting Sort for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i A 3 2 3 C 2 3 6 i=3 1
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 A 3 2 3 C 2 3 6 B 1 3 1
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=6, A[6]=1, C[1]=2 A 3 2 3 C 2 3 6 B 1 3 1
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=6, A[6]=1, C[1]=2 A 3 2 3 C 1 3 6 B 1 1 3 1
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=5, A[5]=3, C[3]=6 A 3 2 3 C 1 3 6 B 1 1 3 1
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=5, A[5]=3, C[3]=6 A 3 2 3 C 1 3 5 B 1 1 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=4, A[4]=1, C[1]=1 A 3 2 3 C 1 3 5 B 1 1 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=4, A[4]=1, C[1]=1 A 3 2 3 C 0 3 5 B 1 1 1 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=3, A[3]=3, C[3]=5 A 3 2 3 C 0 3 5 B 1 1 1 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=3, A[3]=3, C[3]=5 A 3 2 3 C 0 3 4 B 1 1 1 3 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=2, A[2]=2, C[2]=3 A 3 2 3 C 0 3 4 B 1 1 1 3 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=2, A[2]=2, C[2]=3 A 3 2 3 C 0 2 4 B 1 1 2 1 3 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=1, A[1]=3, C[3]=4 A 3 2 3 C 0 2 4 B 1 1 2 1 3 3
Counting Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=1, A[1]=3, C[3]=4 A 3 2 3 C 0 2 3 B 1 1 2 1 3 3 3
Radix sort - example Sort the following numbers in linear time 329, 457, 657, 839, 436, 720, 355 Solution: We have 7 numbers with 3 digits. n=7, d=3, b=10 (decimal)
Radix sort - example 329, 457, 657, 839, 436, 720, 355 1 0 0 1 1 2 C 1 B 720 1 1 355 1 436 1 2 457 3 657 5 329 0 2 5 7 839
Radix sort - example 720, 355, 436, 457, 657, 329, 839 – sorted by digit 1 0 0 2 2 0 3 0 0 C 0 B 720 0 2 329 4 436 4 7 839 7 355 7 457 7 7 657
Radix sort - example 720, 329, 436, 839, 355, 457, 657 – sorted by digit 2 (and 1, inherently) 0 0 0 2 2 0 1 1 1 0 C 0 B 329 0 0 355 2 436 4 4 457 5 657 6 720 7 7 839 329, 355, 436, 457, 657, 720, 839 – sorted
Question 3 - Choosing sorting algorithm efficiently •
Bucket Sort •
Bucket Sort •
Question 2 – an improved Bucket-sort algorithm •
Question 2 – an improved Bucket-sort algorithm •
Question 2 – an improved Bucket-sort algorithm •
Question 4 – sorting a collection of sets efficiently •
Question 4 – sorting a collection of sets efficiently •
Question 4 – sorting a collection of sets efficiently •
Question 5 – Sorting Lexicographically •
Question 5 – Sorting Lexicographically •
Question 6 – sorting input with anomalies •
(Important) Sort Algorithms Review
- Compare selection sort and quick sort algorithm.
- Ngoại tâm thu thất chùm đôi
- Block nhĩ thất độ 1
- Thơ thất ngôn tứ tuyệt đường luật
- Thơ thất ngôn tứ tuyệt đường luật
- Walmart thất bại ở nhật
- Tìm vết của mặt phẳng
- Hãy nói thật ít để làm được nhiều
- Tôn thất thuyết là ai
- Gây tê cơ vuông thắt lưng
- Sau thất bại ở hồ điển triệt
- Bubble sort mpi
- Radix sort vs bucket sort
- Approximate counting algorithm
- Sadlier level d unit 1 synonyms
- Bubble sort vs selection sort
- Difference between selection sort and bubble sort
- Pseudo code for bubble sort
- Difference between selection and insertion sort
- Bubble sort insertion sort
- Compare between bubble sort and selection sort.
- Quick sort merge sort
- Quick sort merge sort
- Bucket sort vs radix sort
- Topological sort kahn's algorithm
- Radix sort algorithm
- Timsort pseudocode
- Insertion sort algorithm flowchart
- Quick sort algorithm
- Topological sort algorithm
- Search sort algorithm
- Bubble sort best case and worst case
- Search sort algorithm
- Quick sort algorithm with example
- Bad sort
- Simplest sort algorithm
- Difference between a star and ao star algorithm
- Sweep line algorithm cp algorithm
- If you are not confused you're not paying attention
- Informal-casual
- Attention is not not explanation
- Too broad too narrow
- If p, then q
- Not too big not too small just right