Don't be scared!
2
[ 8, 3, 5, 4, 7, 6, 1, 2 ]
[ 8, 3, 5, 4 ]
[ 7, 6, 1, 2 ]
[ 8, 3 ]
[ 5, 4 ]
[ 1, 2 ]
[ 7, 6 ]
[ 8 ]
[ 3 ]
[ 5 ]
[ 4 ]
[ 7 ]
[ 6 ]
[ 1 ]
[ 2 ]
[ 3, 8 ]
[ 4, 5 ]
[ 6, 7 ]
[ 1, 2 ]
[ 3, 4, 5, 8 ]
[ 1, 2, 6, 7 ]
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
mergeSort([10,24,76,73])
mergeSort([10,24])
mergeSort([76,73])
mergeSort([76])
mergeSort([73])
mergeSort([10])
mergeSort([24])
[10]
[24]
[10,24]
[73,76]
[76]
[73]
merge
merge
merge
[10,24,73,76]
Time Complexity (Best) | Time Complexity (Average) | Time Complexity (Worst) | Space Complexity |
---|---|---|---|
O(n log n) | O(n log n) | O(n log n) | O(n) |
[ 8 ]
[ 3 ]
[ 5 ]
[ 4 ]
[ 7 ]
[ 6 ]
[ 1 ]
[ 2 ]
[ 3, 8 ]
[ 4, 5 ]
[ 6, 7 ]
[ 1, 2 ]
[ 3, 4, 5, 8 ]
[ 1, 2, 6, 7 ]
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
O(log n) decompositions
O(n) comparisons per decomposition
Why???
[ 5, 2, 1, 8, 4, 7, 6, 3 ]
5
3, 2, 1, 4
7, 6, 8
3
1, 2
7, 6, 8
4
3, 2, 1, 4
1, 2
1
2
7, 6, 8
7, 6, 8
7, 6, 8
7, 6, 8
7
6
8
let arr = [ 5, 2, 1, 8, 4, 7, 6, 3 ]
pivot(arr); // 4;
arr;
// any one of these is an acceptable mutation:
// [2, 1, 4, 3, 5, 8, 7, 6]
// [1, 4, 3, 2, 5, 7, 6, 8]
// [3, 2, 1, 4, 5, 7, 6, 8]
// [4, 1, 2, 3, 5, 6, 8, 7]
// there are other acceptable mutations too!
All that matters is for 5 to be at index 4, for smaller values to be to the left, and for larger values to be to the right
Time Complexity (Best) | Time Complexity (Average) | Time Complexity (Worst) | Space Complexity |
---|---|---|---|
O(n log n) | O(n log n) | O(n ) | O(log n) |
2
Why???
Best Case
[8, 5, 6, 1, 3, 7, 2, 4, 12, 13, 14, 11, 9, 15, 10]
[4, 5, 6, 1, 3, 7, 2]
[12, 13, 14, 11, 9, 15, 10]
8
4
12
[2, 1, 3]
[6, 7, 5]
[10, 11, 9]
[14, 15, 13]
2
1
3
6
5
7
10
9
11
14
15
13
O(n) comparisons per decomposition
O(log n) decompositions
Why???
Worst Case
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
O(n) comparisons per decomposition
O(n) decompositions
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[7, 8, 9, 10, 11, 12, 13, 14, 15]
[8, 9, 10, 11, 12, 13, 14, 15]
[9, 10, 11, 12, 13, 14, 15]
[10, 11, 12, 13, 14, 15]
[11, 12, 13, 14, 15]
[12, 13, 14, 15]
[13, 14, 15]
[14, 15]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
COMPARISON SORTS
Average Time Complexity
Can we do better?
YES,
BUT NOT BY MAKING COMPARISONS
Radix sort is a special sorting algorithm that works on lists of numbers
It exploits the fact that information about the size of a number is encoded in the number of digits.
More digits means a bigger number!
It never makes comparisons between elements!
[1556, 4, 3556, 593, 408, 4386, 902, 7, 8157, 86, 9637, 29]
0
1
2
3
4
5
6
7
8
9
1556
[1556, 4, 3556, 593, 408, 4386, 902, 7, 8157, 86, 9637, 29]
4
3556
593
408
4386
902
7
8157
86
9637
29
[902, 593, 4, 1556, 3556, 4386, 86, 7, 8157, 9637, 408, 29]
[902, 4, 7, 408, 29, 9637, 1556, 3556, 8157, 4386, 86, 593]
902
593
4
1556
3556
4386
86
7
8157
9637
408
29
902
4
7
408
29
9637
1556
3556
8157
4386
86
593
[4, 7, 29, 86, 8157, 4386, 408, 1556, 3556, 593, 9637, 902]
4
7
29
86
8157
4386
408
1556
3556
593
9637
902
[4, 7, 29, 86, 408, 593, 902, 1556, 3556, 4386, 8157, 9637]
In order to implement radix sort, it's helpful to build a few helper functions first:
getDigit(num, place) - returns the digit in num at the given place value
getDigit(12345, 0); // 5
getDigit(12345, 1); // 4
getDigit(12345, 2); // 3
getDigit(12345, 3); // 2
getDigit(12345, 4); // 1
getDigit(12345, 5); // 0
In order to implement radix sort, it's helpful to build a few helper functions first:
getDigit(num, place) - returns the digit in num at the given place value
function getDigit(num, i) {
return Math.floor(Math.abs(num) / Math.pow(10, i)) % 10;
}
In order to implement radix sort, it's helpful to build a few helper functions first:
digitCount(num) - returns the number of digits in num
digitCount(1); // 1
digitCount(25); // 2
digitCount(314); // 3
In order to implement radix sort, it's helpful to build a few helper functions first:
digitCount(num) - returns the number of digits in num
function digitCount(num) {
if (num === 0) return 1;
return Math.floor(Math.log10(Math.abs(num))) + 1;
}
In order to implement radix sort, it's helpful to build a few helper functions first:
mostDigits(nums) - Given an array of numbers, returns the number of digits in the largest numbers in the list
mostDigits([1234, 56, 7]); // 4
mostDigits([1, 1, 11111, 1]); // 5
mostDigits([12, 34, 56, 78]); // 2
In order to implement radix sort, it's helpful to build a few helper functions first:
mostDigits(nums) - Given an array of numbers, returns the number of digits in the largest numbers in the list
function mostDigits(nums) {
let maxDigits = 0;
for (let i = 0; i < nums.length; i++) {
maxDigits = Math.max(maxDigits, digitCount(nums[i]));
}
return maxDigits;
}
Time Complexity (Best) | Time Complexity (Average) | Time Complexity (Worst) | Space Complexity |
---|---|---|---|
O(nk) | O(nk) | O(nk) | O(n + k) |