- Notifications
You must be signed in to change notification settings - Fork932
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:masterChoose a base branch fromAdithyaS99:newbranch
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletionssrc/algorithms/sorting/radix-sort.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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]); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.