code-shoily / algorithms-in-dart

Implementation of data structures and algorithms in Dart programming language.
221 stars 76 forks source link

Radix Sort #37

Closed vipuluthaiah closed 3 years ago

vipuluthaiah commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

NV I i got the solution