L5 Dsa
L5 Dsa
Villacarlos
Sorting II
LOWER-BOUND ON COMPARISON-BASED SORTING, NON-COMPARISON-
BASED SORTING
Comparison-Based Sorting Algorithms
◦Selection, insertion, and mergesort are comparison-based
sorting procedures.
Decision Tree T
𝑎0 < 𝑎2
F
𝑎1 < 𝑎2
F
T
◦Comparison-based algorithms can
be represented as binary
decision-tree 𝑎 <𝑎 1 𝑎 <𝑎 2 𝑎 <𝑎 0 𝑎 <𝑎 1 0 2 0 1
◦ Edges (lines) represents Decision Tree for Selection Sort with 3-element array input
True/False Decision
Sorting 3,1,2 leads to the highlighted decision 𝑎1 , 𝑎2 , 𝑎0 .
The sorted version is 𝑎1 first, 𝑎2 second, and 𝑎0 last
Decision Tree
◦The leaves of the decision trees are the possible permutations
(arrangements) of the 𝑛 elements of the array
◦ The number permutations of 𝑛 elements is equal to 𝑛! = 𝑛 × 𝑛 − 1 ×
⋯× 3 × 2 × 1
◦ If there are 𝑛 = 3 elements then there are 𝑛! = 1 × 2 × 3 = 6 possible
arrangements:
𝑎0 , 𝑎1 , 𝑎2 , 𝑎0 , 𝑎2 , 𝑎1 , 𝑎1 , 𝑎0 , 𝑎2 , 𝑎1 , 𝑎2 , 𝑎0 , 𝑎2 , 𝑎0 , 𝑎1 , 𝑎2 , 𝑎1 , 𝑎0
1 2 2 1
◦Ex: If 𝑇 𝑛 = 𝑛 − 𝑛 then 𝑇 𝑛 = Ω 𝑛 with 𝑐 = and 𝑛0 = 4
2 4
◦If we sort without using comparison, then we can beat the lower-
bound
i i i i i
𝐴 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2
𝐶 0 0 0 0 1 0 0 1 0 1 0 1 1 0 1 0 1 1 0 2 0 1 2 0 2
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
i i i i
𝐵 4 4 2 4 2 1 4 2 1 4 4 2 1 4 2
Counting Sort for (int i = 1; i < C.length; i++){
C[i] = C[i] + C[i-1];
Compute Offset }
𝐴 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2
j j j j
𝐶 0 1 2 0 2 0 1 3 0 2 0 1 3 3 2 0 1 3 3 5
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
𝐵 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2
Counting Sort for (int i = B.length-1; i >=0; i--){
A[C[B[i]] - 1] = B[i];
C[B[i]]--;
Distribute }
𝐴 4 2 2 4 2 4 2 2 4 4 1 2 2 4 4 1 2 2 4 4 1 2 2 4 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
−1 −1 −1 −1 −1
𝐶 0 1 3 3 5 0 1 2 3 5 0 1 2 3 4 0 0 2 3 4 0 0 1 3 4
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
i i i i i
𝐵 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2 4 2 1 4 2
Counting Sort
public static void sort(int A[],int k){
int C[] = new int[k];
int B[] = new int[A.length];
for (int i = 0; i < C.length; i++){
C[i] = 0; 𝑘 steps
}
for (int i = 0; i < A.length; i++){
𝑇 𝑛 =2 𝑛+𝑘 =𝑂 𝑛+𝑘
C[A[i]]++; 𝑛 steps
B[i] = A[i];
}
for (int i = 1; i < C.length; i++){ Counting sort runs in 𝑂 𝑛
C[i] = C[i] + C[i-1]; 𝑘 steps time, if 𝑘 = 𝑂 𝑛 ,
}
for (int i = B.length-1; i >=0; i--){
A[C[B[i]] - 1] = B[i];
C[B[i]]--; 𝑛 steps
}
}
Stable Sorting
◦A sorting procedure is stable if equal elements are ordered based on which
comes first in the array
◦ Ex: If 𝐴 = 1,0,1,2 then a stable sorting procedure should output 0,1,1,2
◦ Mergesort and counting sort are stable sorting algorithms
◦Stable sorting is important when the data needs to be sorted based on multiple
criteria
◦ A stable sorting algorithm will ensure that after sorting the persons by last name,
Aguinaldo, Emilio will still appear first before Jacinto, Emilio when they are sorted by
first name.
Radix Sort
◦Let 𝐴 = 𝑎0 , 𝑎1 , … 𝑎𝑛−1 be an array of integers where each 𝑎𝑖 is in the range 0 to 𝑘 − 1,
where 𝑘 is a power of 10.
◦ Ex: Elements of 𝐴 = 14, 23, 145, 70, 1,15, 520 is from the range 0 to 103 − 1
◦In most implementation of radix sort the stable sort used is counting sort
Radix Sort
◦The number of passes in radix sort is the number of times counting sort is
applied to each 𝑎𝑖 and is equal to the number of columns
◦The number of passes is at most 𝑑 but can be reduced by increasing the digits
per column.
◦ If 𝑑 = 8 and columns contain 2 digits, there will only be 4 columns instead of 8
Radix Sort
◦Let 𝑔 be the number of digits per column.
log10 𝑘
◦Since log 𝑛 𝑘 = , the running-time becomes 𝑂 𝑛 log 𝑛 𝑘 , which is 𝑂 𝑛
log10 𝑛
if 𝑘 ≤ 𝑛𝑐 where 𝑐 ≥ 0