|
| 1 | +import Sort from '../Sort'; |
| 2 | + |
| 3 | +export default class RadixSort extends Sort { |
| 4 | + sort(originalArray) { |
| 5 | + const isNumber = (element) => { |
| 6 | + return Number.isInteger(element); |
| 7 | + }; |
| 8 | + |
| 9 | + const createBuckets = (numBuckets) => { |
| 10 | + /** |
| 11 | + * Mapping buckets to an array instead of filling them with |
| 12 | + * an array prevents each bucket from containing a reference to the same array |
| 13 | + */ |
| 14 | + return new Array(numBuckets).fill(null).map(() => []); |
| 15 | + }; |
| 16 | + |
| 17 | + const placeElementsInNumberBuckets = (array, index) => { |
| 18 | + // See below. These are used to determine which digit to use for bucket allocation |
| 19 | + const modded = 10 ** (index + 1); |
| 20 | + const divided = 10 ** index; |
| 21 | + const buckets = createBuckets(10); |
| 22 | + |
| 23 | + array.forEach((element) => { |
| 24 | + this.callbacks.visitingCallback(element); |
| 25 | + if (element < divided) { |
| 26 | + buckets[0].push(element); |
| 27 | + } else { |
| 28 | + /** |
| 29 | + * Say we have element of 1,052 and are currently on index 1 (starting from 0). This means |
| 30 | + * we want to use '5' as the bucket. `modded` would be 10 ** (1 + 1), which |
| 31 | + * is 100. So we take 1,052 % 100 (52) and divide it by 10 (5.2) and floor it (5). |
| 32 | + */ |
| 33 | + const currentDigit = Math.floor((element % modded) / divided); |
| 34 | + buckets[currentDigit].push(element); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + return buckets; |
| 39 | + }; |
| 40 | + |
| 41 | + const placeElementsInCharacterBuckets = (array, index, numPasses) => { |
| 42 | + const getCharCodeOfElementAtIndex = (element) => { |
| 43 | + // Place element in last bucket if not ready to organize |
| 44 | + if ((numPasses - index) > element.length) return 25; |
| 45 | + // Using charCode (a = 97, b = 98, etc), we can map characters to buckets from 0 - 25 |
| 46 | + const BASE_CHAR_CODE = 97; |
| 47 | + /** |
| 48 | + * If each character has been organized, use first character to determine bucket, |
| 49 | + * otherwise iterate backwards through element |
| 50 | + */ |
| 51 | + const charPos = index > element.length - 1 ? 0 : element.length - index - 1; |
| 52 | + |
| 53 | + return element.toLowerCase().charCodeAt(charPos) - BASE_CHAR_CODE; |
| 54 | + }; |
| 55 | + |
| 56 | + const buckets = createBuckets(26); |
| 57 | + |
| 58 | + array.forEach((element) => { |
| 59 | + this.callbacks.visitingCallback(element); |
| 60 | + const currentBucket = getCharCodeOfElementAtIndex(element); |
| 61 | + buckets[currentBucket].push(element); |
| 62 | + }); |
| 63 | + |
| 64 | + return buckets; |
| 65 | + }; |
| 66 | + |
| 67 | + // Assumes all elements of array are of the same type |
| 68 | + const isArrayOfNumbers = isNumber(originalArray[0]); |
| 69 | + |
| 70 | + /** Number of passes is determined by the length of the longest element in the array. |
| 71 | + * For integers, this log10(num), and for strings, this would be the lenght of the string. |
| 72 | + */ |
| 73 | + const determineNumPasses = () => { |
| 74 | + const getLengthOfLongestElement = () => { |
| 75 | + if (isArrayOfNumbers) { |
| 76 | + return Math.floor(Math.log10(Math.max(...originalArray))) + 1; |
| 77 | + } |
| 78 | + |
| 79 | + return originalArray.reduce((acc, val) => { |
| 80 | + return val.length > acc ? val.length : acc; |
| 81 | + }, -Infinity); |
| 82 | + }; |
| 83 | + |
| 84 | + return getLengthOfLongestElement(originalArray); |
| 85 | + }; |
| 86 | + |
| 87 | + let sortedArray = [...originalArray]; |
| 88 | + const numPasses = determineNumPasses(); |
| 89 | + |
| 90 | + for (let currentIndex = 0; currentIndex < numPasses; currentIndex += 1) { |
| 91 | + const buckets = isArrayOfNumbers ? |
| 92 | + placeElementsInNumberBuckets(sortedArray, currentIndex) : |
| 93 | + placeElementsInCharacterBuckets(sortedArray, currentIndex, numPasses); |
| 94 | + // Flatten buckets into sortedArray, and repeat at next index |
| 95 | + sortedArray = buckets.reduce((acc, val) => { |
| 96 | + return [...acc, ...val]; |
| 97 | + }, []); |
| 98 | + } |
| 99 | + |
| 100 | + return sortedArray; |
| 101 | + } |
| 102 | +} |
0 commit comments