samedi 2 décembre 2017

Radix sort function won't work for base 2?

I am writing a program that takes an array of integers and sorts them for bases 16 8 10 and 2. My function works for all bases except 2. Its not sorting the numbers correctly. I have been looking around and no place particularly explains the problem. I think im supposed to use shift (>> <<) but I dont know exactly how?

void Sort(int AR[], int sizeAR, int _base){

queue<int> bins[10]; //bins
int MAX =4; // MAX digits 
int counter=0;// position of counter

while (counter < MAX) {
    for(int i=0; i<sizeAR; i++){// sorts array 
        int dividor=pow(_base,counter);
        int temp = AR[i]; 
        int val = static_cast<int>((temp/dividor)%10); //gets the decimal  
        bins[val].push(temp); 
    }

Aucun commentaire:

Enregistrer un commentaire