|
| 1 | +/** |
| 2 | + * Copyright (C) 2014 Tayllan Búrigo |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to |
| 6 | + * deal in the Software without restriction, including without limitation the |
| 7 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | + * IN THE SOFTWARE. |
| 21 | + */ |
| 22 | +'use strict'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Sorts an array of objects according to their 'key' property |
| 26 | + * Every object inside the array MUST have the 'key' property with |
| 27 | + * a integer value. |
| 28 | + * |
| 29 | + * Asymptotic Complexity: O(array.length * d), where 'd' represents |
| 30 | + * the amount of digits in the larger key of the array |
| 31 | + * |
| 32 | + * @param Array |
| 33 | + * @return Array |
| 34 | + */ |
| 35 | +var radixSort = function (array) { |
| 36 | + var max = maximumKey(array); |
| 37 | + var digitsMax = amountOfDigits(max); |
| 38 | + |
| 39 | + for (var i = 0; i < digitsMax; i++) { |
| 40 | + array = auxiliaryCountingSort(array, i); |
| 41 | + } |
| 42 | + |
| 43 | + return array; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Auxiliary sorting method for RadixSort |
| 48 | + * Sorts an array of objects according to only one digit of |
| 49 | + * their 'key' property. The digit to be sorted is determined |
| 50 | + * by the 'mod' variable |
| 51 | + * Every object inside the array MUST have the 'key' property with |
| 52 | + * a integer value. |
| 53 | + * |
| 54 | + * Execution Time: (2 * array.length + 10) |
| 55 | + * Asymptotic Complexity: O(array.length) |
| 56 | + * |
| 57 | + * @param Array |
| 58 | + * @return Array |
| 59 | + */ |
| 60 | +var auxiliaryCountingSort = function (array, mod) { |
| 61 | + var length = array.length; |
| 62 | + var bucket = []; |
| 63 | + |
| 64 | + for (var i = 0; i < 10; i++) { |
| 65 | + bucket[i] = []; |
| 66 | + } |
| 67 | + |
| 68 | + for (i = 0; i < length; i++) { |
| 69 | + var digit = parseInt((array[i].key / Math.pow(10, mod)).toFixed(mod)) % 10; |
| 70 | + bucket[digit].push(array[i]); |
| 71 | + } |
| 72 | + |
| 73 | + var pointer = 0; |
| 74 | + |
| 75 | + for (i = 0; i < 10; i++) { |
| 76 | + var localLength = bucket[i].length; |
| 77 | + |
| 78 | + for (var j = 0; j < localLength; j++) { |
| 79 | + array[pointer++] = bucket[i][j]; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return array; |
| 84 | +}; |
| 85 | + |
| 86 | +/** |
| 87 | + * Finds the maximum key from an array of objects |
| 88 | + * |
| 89 | + * Asymptotic Complexity: O(array.length) |
| 90 | + * |
| 91 | + * @param Array |
| 92 | + * @return Integer if array non-empty |
| 93 | + * Undefined otherwise |
| 94 | + */ |
| 95 | +var maximumKey = function (array) { |
| 96 | + var length = array.length; |
| 97 | + |
| 98 | + if (length > 0) { |
| 99 | + var max = array[0].key; |
| 100 | + |
| 101 | + for (var i = 1; i < length; i++) { |
| 102 | + if (array[i].key > max) { |
| 103 | + max = array[i].key; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return max; |
| 108 | + } |
| 109 | + else { |
| 110 | + return undefined; |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +/** |
| 115 | + * Returns the amount of digits contained in a number |
| 116 | + * |
| 117 | + * Asymptotic Complexity: O(d), where 'd' represents the |
| 118 | + * amount of digits in the number |
| 119 | + * |
| 120 | + * @param Number |
| 121 | + * @return Number |
| 122 | + */ |
| 123 | +var amountOfDigits = function (number) { |
| 124 | + if (number === 0) { |
| 125 | + return 1; |
| 126 | + } |
| 127 | + else { |
| 128 | + var counter = 0; |
| 129 | + |
| 130 | + // For positive numbers |
| 131 | + while (parseInt(number) > 0) { |
| 132 | + number /= 10; |
| 133 | + ++counter; |
| 134 | + } |
| 135 | + |
| 136 | + // For negative numbers |
| 137 | + while (parseInt(number) < 0) { |
| 138 | + number /= 10; |
| 139 | + ++counter; |
| 140 | + } |
| 141 | + |
| 142 | + return counter; |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +module.exports = radixSort; |
0 commit comments