From a7a848f6fb116835e68a3b554ef959240e64bb7e Mon Sep 17 00:00:00 2001 From: G-prog <54137657+G-prog@users.noreply.github.com> Date: Wed, 30 Oct 2019 18:08:50 +0900 Subject: [PATCH] Create radixSort.js --- src/algorithms/sorting/radixSort.js | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/algorithms/sorting/radixSort.js diff --git a/src/algorithms/sorting/radixSort.js b/src/algorithms/sorting/radixSort.js new file mode 100644 index 0000000..2346e42 --- /dev/null +++ b/src/algorithms/sorting/radixSort.js @@ -0,0 +1,66 @@ +var testArray = [ 331, 454, 230, 34, 343, 45, 59, 453, 345, 231, 9 ]; + + function radixBucketSort (arr) { + var idx1, idx2, idx3, len1, len2, radix, radixKey; + var radices = {}, buckets = {}, num, curr; + var currLen, radixStr, currBucket; + + len1 = arr.length; + len2 = 10; // radix sort uses ten buckets + + // find the relevant radices to process for efficiency + for (idx1 = 0;idx1 < len1;idx1++) { + radices[arr[idx1].toString().length] = 0; + } + + // loop for each radix. For each radix we put all the items + // in buckets, and then pull them out of the buckets. + for (radix in radices) { + // put each array item in a bucket based on its radix value + len1 = arr.length; + for (idx1 = 0;idx1 < len1;idx1++) { + curr = arr[idx1]; + // item length is used to find its current radix value + currLen = curr.toString().length; + // only put the item in a radix bucket if the item + // key is as long as the radix + if (currLen >= radix) { + // radix starts from beginning of key, so need to + // adjust to get redix values from start of stringified key + radixKey = curr.toString()[currLen - radix]; + // create the bucket if it does not already exist + if (!buckets.hasOwnProperty(radixKey)) { + buckets[radixKey] = []; + } + // put the array value in the bucket + buckets[radixKey].push(curr); + } else { + if (!buckets.hasOwnProperty('0')) { + buckets['0'] = []; + } + buckets['0'].push(curr); + } + } + // for current radix, items are in buckets, now put them + // back in the array based on their buckets + // this index moves us through the array as we insert items + idx1 = 0; + // go through all the buckets + for (idx2 = 0;idx2 < len2;idx2++) { + // only process buckets with items + if (buckets[idx2] != null) { + currBucket = buckets[idx2]; + // insert all bucket items into array + len1 = currBucket.length; + for (idx3 = 0;idx3 < len1;idx3++) { + arr[idx1++] = currBucket[idx3]; + } + } + } + buckets = {}; + } + } + radixBucketSort(testArray); + console.dir(testArray); + +//Refernce : https://stackoverflow.com/questions/36513414/javascript-radix-sort