Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Radix sort in javascript#108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
AdithyaS99 wants to merge4 commits intoamejiarosario:master
base:master
Choose a base branch
Loading
fromAdithyaS99:newbranch
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletionssrc/algorithms/sorting/radix-sort.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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;
}
// unit test
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]))
35 changes: 35 additions & 0 deletionssrc/algorithms/sorting/radix-sort.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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]);
});
});

[8]ページ先頭

©2009-2025 Movatter.jp