কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে রেডিক্স সর্ট (Radix Sort) বাস্তবায়ন

রেডিক্স সর্ট অ্যালগরিদম পূর্ণসংখ্যাগুলো বালতি (buckets)তে বিতরণ করে একটি সংখ্যার গুরুত্বপূর্ণ ডিজিট বা ভ্যালু (রেডিক্স) এর উপর ভিত্তি করে। রেডিক্স অ্যারে মানগুলোর নাম্বার সিস্টেমের উপর ভিত্তি করে। নিচে কীভাবে বাস্তবায়ন করা যায় তা দেখানো হলো:

উদাহরণ

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 concatenating all sub arrays 
    arr = [].concat.apply([], buckets); 
    // Move to the next significant digit 
    divisor *= 10; 
  } 
  return arr; 
} 
console.log(radixSort([5,3,88,235,65,23,4632,234]))

আউটপুট

[ 3, 5, 23, 65, 88, 234, 235, 4632 ]
  1. জাভাস্ক্রিপ্ট রেজেক্সে \d বনাম \D পার্থক্য

  2. জাভাস্ক্রিপ্ট Sort() মেথড

  3. জাভাস্ক্রিপ্টে Array.prototype.sort() মেথড

  4. C প্রোগ্রামিং-এ রেডিক্স সর্ট (Radix Sort)