First of all this is homework , and I found another topic talking about the same subject but there was no answer. Here is the problem:
Sorting by bit based on the assumption that the values to be sorted are integers coded B bits (and therefore between 0 and 2B-1).
The main problem is how to make this kind of sort. Should I convert each integer to bits and compare them? Please do not give me the solution just a hint or an explanation of how to do it. Thanks for your help ! [EDIT] I found this script in the internet but i did not understand how it works :
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include<algorithm>
#include<string>
#include <iterator>
using namespace std;
// Radix sort comparator for 32-bit two's complement integers
class radix_test
{
const int bit; // bit position [0..31] to examine
public:
radix_test(int offset) : bit(offset) {} // constructor
bool operator()(int value) const // function call operator
{
if (bit == 31) // sign bit
return value < 0; // negative int to left partition
else
return !(value & (1 << bit)); // 0 bit to left partition
}
};
// Least significant digit radix sort
void lsd_radix_sort(int *first, int *last)
{
for (int lsb = 0; lsb < 32; ++lsb) // least-significant-bit
{
std::stable_partition(first, last, radix_test(lsb));
}
}
// Most significant digit radix sort (recursive)
void msd_radix_sort(int *first, int *last, int msb = 31)
{
if (first != last && msb >= 0)
{
int *mid = std::partition(first, last, radix_test(msb));
msb--; // decrement most-significant-bit
msd_radix_sort(first, mid, msb); // sort left partition
msd_radix_sort(mid, last, msb); // sort right partition
}
}
int main(int argc, char *argv[])
{
int data[] = { 170, 45, 75, -90, -802, 24, 2, 66 };
lsd_radix_sort(data, data + 8);
// msd_radix_sort(data, data + 8);
std::copy(data, data + 8, std::ostream_iterator<int>(std::cout, " "));
system("PAUSE");
return EXIT_SUCCESS;
}
First of all, you don't need to convert an integer to bits, because it already is stored as bits. An
int
is usually 4 bytes, so 32 bits. You can access the bits using bit operators.Radix sort is shown here in detail. https://en.wikipedia.org/wiki/Radix_sort
This example sorts based on base 10 digits.
To sort based on bit, you would change the algorithm slightly to use 2 instead of 10 in all places:
But if you needed to use bit wise operators instead, you could recognize that anything divided by 2 is simply
>> 1
, multiply by 2 is<< 1
, and modulo 2 is&1
. By replacingexp
with the bit position, we can rewrite as follows:This sorts using a single bit. To use multiple bits, you'd need to make it more generic:
This sorts using two bits, so 4 buckets are used for 00, 01, 10, and 11. 3 bits would use 8 buckets (000, 001, 010, 011, 100, 101, 110, 111).
You can see how increasing the BITS will make fewer passes, but the work done in each pass is larger.