From 67e94eff8a00157cb09d05a56f99766bfbb7258f Mon Sep 17 00:00:00 2001 From: Adithya Date: Fri, 19 Nov 2021 17:30:50 +0530 Subject: [PATCH 1/4] Radix sort in javascript --- src/algorithms/sorting/radix-sort.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/algorithms/sorting/radix-sort.js diff --git a/src/algorithms/sorting/radix-sort.js b/src/algorithms/sorting/radix-sort.js new file mode 100644 index 00000000..169376b1 --- /dev/null +++ b/src/algorithms/sorting/radix-sort.js @@ -0,0 +1,19 @@ +function radixSort(arr) { + // Find the max number and multiply it by 10 to get a number + // with no. of digits of max + 1 + const maxNum = Math.max(...arr) * 10; + let divisor = 10; + while (divisor < maxNum) { + // Create bucket arrays for each of 0-9 + let buckets = [...Array(10)].map(() => []); + // For each number, get the current significant digit and put it in the respective bucket + for (let num of arr) { + buckets[Math.floor((num % divisor) / (divisor / 10))].push(num); + } + // Reconstruct the array by concatinating all sub arrays + arr = [].concat.apply([], buckets); + // Move to the next significant digit + divisor *= 10; + } + return arr; + } \ No newline at end of file From 9d48a1890e8a1a2510d2ec0a129f013453b6948b Mon Sep 17 00:00:00 2001 From: Adithya Date: Fri, 19 Nov 2021 20:12:16 +0530 Subject: [PATCH 2/4] added unit test --- src/algorithms/sorting/radix-sort.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/algorithms/sorting/radix-sort.js b/src/algorithms/sorting/radix-sort.js index 169376b1..6072b87e 100644 --- a/src/algorithms/sorting/radix-sort.js +++ b/src/algorithms/sorting/radix-sort.js @@ -16,4 +16,6 @@ function radixSort(arr) { divisor *= 10; } return arr; - } \ No newline at end of file + } +// unit test +console.log(radixSort([5,3,88,235,65,23,4632,234])) \ No newline at end of file From 6488de0bdd1e34a3ef6f7da7e39a583a56b350aa Mon Sep 17 00:00:00 2001 From: Adithya Date: Fri, 19 Nov 2021 20:15:29 +0530 Subject: [PATCH 3/4] added 2 more unit tests --- src/algorithms/sorting/radix-sort.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/algorithms/sorting/radix-sort.js b/src/algorithms/sorting/radix-sort.js index 6072b87e..5e6bd356 100644 --- a/src/algorithms/sorting/radix-sort.js +++ b/src/algorithms/sorting/radix-sort.js @@ -18,4 +18,8 @@ function radixSort(arr) { return arr; } // unit test -console.log(radixSort([5,3,88,235,65,23,4632,234])) \ No newline at end of file +console.log(radixSort([5,3,88,235,65,23,4632,234])) +// unit test 2 +console.log(radixSort([802, 630, 20, 745, 52, 300, 612, 932, 78, 187])) +// unit test 3 +console.log(radixSort([45, 2, 56, 2, 5, 6, 34, 1, 56, 89, 33])) \ No newline at end of file From c573063b8d1cc8812593cc6cbf8e3c792c955e5f Mon Sep 17 00:00:00 2001 From: Adithya Date: Sat, 20 Nov 2021 11:43:19 +0530 Subject: [PATCH 4/4] added separate unit test file --- src/algorithms/sorting/radix-sort.test.js | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/algorithms/sorting/radix-sort.test.js diff --git a/src/algorithms/sorting/radix-sort.test.js b/src/algorithms/sorting/radix-sort.test.js new file mode 100644 index 00000000..1de06da3 --- /dev/null +++ b/src/algorithms/sorting/radix-sort.test.js @@ -0,0 +1,35 @@ +const radixSort = require('./radix-sort'); + +describe('Testing Radix Sort Algorithm', () => { + it('should sort with multiple digit elements #1', () => { + expect(radixSort([5,3,88,235,65,23,4632,234])).toEqual([3,5,23,65,88,234,235,4632]); + }); + + it('should sort with multiple digit elements #2', () => { + expect(radixSort([802,630,20,745,52,300,612,932,78,187])).toEqual([20,52,78,187,300,612,630,802,932]); + }); + + it('should sort with duplicated values', () => { + expect(radixSort([45,2,56,2,5,6,34,1,56,89,33])).toEqual([1,2,2,5,6,33,34,45,56,56,89]); + }); + + it('should work with zero numbers', () => { + expect(radixSort([])).toEqual([]); + }); + + it('should work with one number', () => { + expect(radixSort([3])).toEqual([3]); + }); + + it('should sort numbers', () => { + expect(radixSort([3, 5, 0])).toEqual([0, 3, 5]); + }); + + it('should sort with inverse array', () => { + expect(radixSort([3, 2, 1])).toEqual([1, 2, 3]); + }); + + it('should sort with already sorted array', () => { + expect(radixSort([1, 2, 3])).toEqual([1, 2, 3]); + }); +}); \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy