Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radix Sort #37

Closed
vipuluthaiah opened this issue Nov 27, 2020 · 4 comments
Closed

Radix Sort #37

vipuluthaiah opened this issue Nov 27, 2020 · 4 comments

Comments

@vipuluthaiah
Copy link

I am watching dsa js tutorial and implementing in on dart currently on radix sort

//radix sort
import 'dart:math' as Math;

main() {
}

getDigitNum(int n, int i) {
// return ((n / 100).round() %10);
// var cal = (n.abs() / Math.pow(10, =i)) % 10;
var cal = (n.round().abs() / Math.pow(10, i)) % 10;
// var cal = (n / 100)%10;

return cal.round();
}

digitCount(int number) {
if (number == 0) {
return 1;
}
return (number.abs().toString().length);
}

mostDigits(List number) {
var maxDigits = 0;
for (var i = 0; i < number.length; i++) {
maxDigits = Math.max(maxDigits, digitCount(number[i]));
}
return maxDigits;
}

radixSort(List nums) {
var maxDightCount = mostDigits(nums);
for (var k = 0; k < maxDightCount; k++) {
List digitBuckets = List.from({});//confused here
}
}

//here is the js codefunction getDigit(num, i) {
return Math.floor(Math.abs(num) / Math.pow(10, i)) % 10;
}

function digitCount(num) {
if (num === 0) return 1;
return Math.floor(Math.log10(Math.abs(num))) + 1;
}

function mostDigits(nums) {
let maxDigits = 0;
for (let i = 0; i < nums.length; i++) {
maxDigits = Math.max(maxDigits, digitCount(nums[i]));
}
return maxDigits;
}

function radixSort(nums){
let maxDigitCount = mostDigits(nums);
for(let k = 0; k < maxDigitCount; k++){
let digitBuckets = Array.from({length: 10}, () => []);
for(let i = 0; i < nums.length; i++){
let digit = getDigit(nums[i],k);
digitBuckets[digit].push(nums[i]);
}
nums = [].concat(...digitBuckets);
}
return nums;
}

radixSort([23,345,5467,12,2345,9852])

//i just want help this part below js code

let digitBuckets = Array.from({length: 10}, () => []);

//dart code
List digitBuckets = List.from({});//confused here

@code-shoily
Copy link
Owner

I did not understand what you are trying to say here, there is a Radix Sort algorithm here, maybe you could look into that and draw parallel between them?

@vipuluthaiah
Copy link
Author

u mixed two algorithm its very hard to follow up!
This is js Radix Sort
carbon (11)
and this dart radix sort
carbon (9)

@vipuluthaiah
Copy link
Author

now i am struck at
this is js way
nums = [].concat(...digitBuckets);//i want this line of code !How can i write this in dart?
//how can i concat and use spread operator in dart?
nums=[].addAll(...digitBucketsd);//facing problem here and confused

@vipuluthaiah
Copy link
Author

NV I i got the solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants