The implementation of Radix sort algorithm using buckets in C++.
The algorithm was MCA practical question for Advanced Algorithm Design paper.
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
/*
Coded by AJITH K P
(c) _TERMINAL_CODERS_ (c)
*/
int* radixSort(int *numbers, int size){
int radix = 0;
queue<int> bucket[10];
bool flg = true;
while(flg){
flg = false;
int divisor = pow(10, radix);
for(int i=0;i<size;i++){
int divideResult = numbers[i] / divisor;
if(divideResult>0){
flg = true;
}
bucket[(divideResult)%10].push(numbers[i]);
}
int ind = 0;
for(int i=0;i<10;i++){
while(!bucket[i].empty()){
numbers[ind++] = bucket[i].front();
bucket[i].pop();
}
}
radix++;
}
return numbers;
}
void display(int *numbers, int size){
for(int i=0;i<size;i++){
cout<<numbers[i]<<" ";
}
}
int main(){
int *numbers, size;
cout<<"Enter size of array: ";
cin>>size;
cout<<"Enter "<<size<<" numbers: ";
for(int i=0;i<size;i++){
cin>>numbers[i];
}
numbers = radixSort(numbers, size);
display(numbers, size);
return 0;
}