Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6758487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:47:46+00:00 2026-05-26T13:47:46+00:00

I need some help implementing the Radix sort algorthim in JavaScript. I found this

  • 0

I need some help implementing the Radix sort algorthim in JavaScript.

I found this example online, with the following code, but I don’t understand how I call the function since it appears to be tailored for that site:

// Radix sort a (base 2)
// Numbers must be in the range 0 to 2**31 - 1
function radixSort() {
  readArray('i');
  var b0 = new obj();  // Bin for 0 digits
  var b1 = new obj();  // Bin for 1 digits

  for (var i=0; i<32; ++i) {
    if (form.step.checked) {  // Single step
      writeArray('i','a');

      if (!confirm("Sort on bit "+i))
        return;    
    }

    var mask = 1<<i;     // Digit (2**i)
    var biggest = 2<<i;  // If all of a is smaller, we're done
    var zeros=0;         // Number of elements in b0, b1
    var ones=0;
    var found=false;     // Any digits past i?

    for (var j=0; j<n; ++j) { // Sort into bins b0, b1
      if ((a[j] & mask) == 0)
        b0[zeros++] = a[j];
      else
        b1[ones++] = a[j];

      if (a[j]>=biggest)  // Any more digits to sort on?
        found=true;
    }

    for (j=0; j<zeros; ++j)  // Concatenate b0, b1 back to a
      a[j]=b0[j];

    for (j=0; j<ones; ++j)
      a[j+zeros]=b1[j];

    form.imoves.value = parseInt(form.imoves.value)+n;

    if (!found)
      break;
  }

  writeArray('i','a');
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:47:47+00:00Added an answer on May 26, 2026 at 1:47 pm

    The term “radix sort” is a tricky one. There are actually two different sorts that work in a similar manner – MSB (most significant bit) radix and LSB (least significant bit) radix. (You will sometimes see the B replaced with a D for digit). Here are implementations of both.

    MSB radix:

    //arguments to sort an array:
    //arr: array to be sorted
    //begin: 0
    //end: length of array
    //bit: maximum number of bits required to represent numbers in arr
    function sort(arr, begin, end, bit)
    {
      var i, j, mask;
      i = begin;
      j = end;
      mask = 1 << bit;
      while(i < j)
      {
        while(i < j && !(arr[i] & mask))
        {
          ++i;
        }
        while(i < j && (arr[j - 1] & mask))
        {
          --j;
        }
        if(i < j)
        {
          j--;
          var tmp = arr[i];
          arr[i] = arr[j];
          arr[j] = tmp;
          i++;
        }
      }
      if(bit && i > begin)
      {
        sort(arr, begin, i, bit - 1);
      }
      if(bit && i < end)
      {
        sort(arr, i, end, bit - 1);
      }
    }
    sort(arr, 0, arr.length, 32);  //Here I've assumed that the values in arr are integers that fit in 32 bits
    

    LSB radix:

    function insert(arr, i, j)
    {
      tmp = arr[i];
      arr.splice(i, 1);
      arr.splice(j, 0, tmp);
    }
    
    //arguments to sort an array:
    //arr: array to be sorted
    function sort(arr)
    {
      var bit, end, i, mask;
      bit = 0;
      while(true) 
      {
        mask = 1 << bit;
        i = 0;
        end = arr.length;
        while(i < end)
        {
          if(arr[i] & mask)
          {
            insert(arr, i, arr.length - 1);
            end--;
          }
          else
          {
            i++;
          }
        }
        bit++;
        if(end === arr.length)
        {
          break;
        }
      }
    }
    

    I pulled these algorithms off of http://visualsort.appspot.com/. Then I compiled them to javascript at http://jashkenas.github.com/coffee-script/, and wrote the insert method/reformatted for readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help with this problem in implementing with XSLT, I had already implemented
Need some help to solve this. I have a gridview and inside the gridview
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I would love some help on a particular design. This is the code I
I need some help implementing a custom role provider in a asp.net mvc app.
I am a novice jQuery user and i need some help in implementing a
I need some help with correctly implementing the USING statement with strongly typed TableAdapters
I'm looking to implement the Shunting-yard Algorithm , but I need some help figuring
I need some help implementing threading using Python 2.1. I would readily update to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.