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 8145697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:45:03+00:00 2026-06-06T13:45:03+00:00

i wanted to know the logic of following radix sort program. #include <stdio.h> #include

  • 0

i wanted to know the logic of following radix sort program.

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

typedef unsigned uint;
#define swap(a, b) { tmp = a; a = b; b = tmp; }
#define each(i, x) for (i = 0; i < x; i++)

/* sort unsigned ints */
static void rad_sort_u(uint *from, uint *to, uint bit)
{
    if (!bit || to < from + 1) return;

uint *ll = from, *rr = to - 1, tmp;
while (1) {
    /* find left most with bit, and right most without bit, swap */
    while (ll < rr && !(*ll & bit)) ll++;
    while (ll < rr &&  (*rr & bit)) rr--;
    if (ll >= rr) break;
    swap(*ll, *rr);
}

if (!(bit & *ll) && ll < to) ll++;
bit >>= 1;

rad_sort_u(from, ll, bit);
rad_sort_u(ll, to, bit);
}

/* sort signed ints: flip highest bit, sort as unsigned, flip back */
static void radix_sort(int *a, const size_t len)
{
size_t i;
uint *x = (uint*) a;

each(i, len) x[i] ^= INT_MIN;
rad_sort_u(x, x + len, INT_MIN);
each(i, len) x[i] ^= INT_MIN;
}

static inline void radix_sort_unsigned(uint *a, const size_t len)
{
rad_sort_u(a, a + len, (uint)INT_MIN);
}

int main(void)
{
int len = 16, x[16], i;
size_t len = 16, i;
each(i, len) x[i] = rand() % 512 - 256;

radix_sort(x, len);

each(i, len) printf("%d%c", x[i], i + 1 < len ? ' ' : '\n');

return 0;
}

i am stuck because i dont quite understand the while(1) loop..

so far what i have come to know is :

INT_MIN=-2147483648

this is same as value of bit in rad_short_u()

i had debugged the program, due to rand() % 512-256, some -ve values are also generated,

during first pass it swaps all -ve values to one side(from starting) and +ve after that
from next pass it shifts left to 1 bit so bit’s value becomes 1073741824 from then to until it becomes 1 array remains same.

please help me to understand the program logic.

  • 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-06-06T13:45:05+00:00Added an answer on June 6, 2026 at 1:45 pm

    To understand this program, you need to understand both quicksort and most-significant-bit radix sort.

    Like quicksort, it partitions the array into parts, then recursively sorts the parts. It first partitions based on the value of the most significant bit. Then it recurses on both halves. But this time, for each half, it partitions based on the second most significant bit. Then it divides again, and for each 1/4, it partitions on the 3rd most significant bit…

    Note that while I am saying “1/2”, “1/4”, and so on, it will not typically divide the array into exactly 1/2, 1/4, etc. The size of each division will depend on the distribution of numbers in the array. For a normal quicksort, the size of each division would depend on the element chosen as “pivot”, but that is not true for this “radix quicksort” — the sequence of “pivots” is fixed.

    Note, also, that unlike a normal quicksort, which can go quadratic and become very slow on certain inputs, this “quicksort” is guaranteed to finish in a fixed number of passes. Actually, the number of passes required is a constant, no matter the input. (This is a typical property of radix sorts — performance tends to be insensitive to the input.)

    Another interesting property: a normal quicksort would divide the array into 3 parts — those lesser, equal, and greater than the pivot. But this “quicksort” always divides its input into exactly 2 parts on each pass — those with a 0 bit in the position being tested, and those with a 1 bit.

    I think the name of this algorithm is actually “binary quicksort”.

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

Sidebar

Related Questions

I wanted know how the kernel is providing memory for simple C program .
Wanted to know if there was a way one could query shelveset details from
Wanted to know if someone had a suggestion on code or maybe there's a
I wanted to know about Data Type implementation in PHP so I need a
I wanted to know that if I am doing correctly, regarding retain and release
I wanted to know if it's possible to derive a method to generate a
Just wanted to know how i would replace sitename.com with sitename2.com whenever a user
I wanted to know how to represent a whitespace character in C#. I found
I wanted to know that how can we find how much time android device
Hi all I wanted to know when I should prefer writing stored procedures over

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.