mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-06-14 22:27:45 +08:00
Performed changes in radix_sort2.cpp
This commit is contained in:
@@ -3,22 +3,22 @@
|
|||||||
* @brief Algorith of [Radix sort](https://en.wikipedia.org/wiki/Radix_sort)
|
* @brief Algorith of [Radix sort](https://en.wikipedia.org/wiki/Radix_sort)
|
||||||
* @author [Suyash Jaiswal](https://github.com/Suyashjaiswal)
|
* @author [Suyash Jaiswal](https://github.com/Suyashjaiswal)
|
||||||
* @details
|
* @details
|
||||||
* Sort the vector of integers using radix sort i.e. sorting digi by digit using count sort as subroutine.
|
* Sort the vector of integers using radix sort i.e. sorting digit by digit using count sort as subroutine.
|
||||||
* Running time of radix is O(d*(n+b)) where b is the base for representing numbers and d in the max digits in input inegers and n is number of integers.
|
* Running time of radix is O(d*(n+b)) where b is the base for representing numbers and d in the max digits in input inegers and n is number of integers.
|
||||||
* consider example for n = 5, array elements = 432,234,143,332,123
|
* consider example for n = 5, array elements = 432,234,143,332,123
|
||||||
sorting digit by digit
|
*sorting digit by digit
|
||||||
sorting according to
|
*sorting according to
|
||||||
1) 1st digit place
|
*1) 1st digit place
|
||||||
=> 432,332,143,123,234
|
*=> 432,332,143,123,234
|
||||||
|
*
|
||||||
2) 2nd digit place
|
*2) 2nd digit place
|
||||||
=> 123,432,332,234,143
|
*=> 123,432,332,234,143
|
||||||
|
*
|
||||||
3) 3rd digit place
|
*3) 3rd digit place
|
||||||
=> 123,143,234,332,432
|
*=> 123,143,234,332,432
|
||||||
|
*
|
||||||
using count sort at each step, which is stable.
|
*using count sort at each step, which is stable.
|
||||||
stable => already sorted according to previous digits.
|
*stable => already sorted according to previous digits.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -48,61 +48,85 @@ namespace sort
|
|||||||
* @brief Constructor that initializes the vector on creation.
|
* @brief Constructor that initializes the vector on creation.
|
||||||
* @param a vector to be sorted.
|
* @param a vector to be sorted.
|
||||||
*/
|
*/
|
||||||
radix(std::vector<int>& a){
|
radix(const std::vector<int>& a){
|
||||||
ar = a;
|
ar = a;
|
||||||
n = ar.size();
|
n = ar.size();
|
||||||
}
|
}
|
||||||
int get_max(){ // returns the max element.
|
/**
|
||||||
|
* @brief Function that returns the max element.
|
||||||
|
* @returns none
|
||||||
|
*/
|
||||||
|
int get_max(){
|
||||||
int mx;for(int i = mx = 0;i < n;i++)mx = std::max(mx,ar[i]);
|
int mx;for(int i = mx = 0;i < n;i++)mx = std::max(mx,ar[i]);
|
||||||
return mx;
|
return mx;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief Function that print the elements of vector.
|
||||||
|
* @returns none
|
||||||
|
*/
|
||||||
void show(){
|
void show(){
|
||||||
for(int i = 0;i < n;i++)std::cout<<ar[i]<<" ";std::cout<<"\n";
|
for(int i = 0;i < n;i++)std::cout<<ar[i]<<" ";std::cout<<"\n";
|
||||||
}
|
}
|
||||||
void radix_sort();
|
void radix_sort();
|
||||||
void step_ith(int dig);
|
void step_ith(int dig);
|
||||||
};
|
};
|
||||||
void radix :: step_ith(int cur_digit){ // sorting according to current digit.
|
/**
|
||||||
int position[10] = {0};
|
* @brief Function to sort vector according to current digit using stable sorting.
|
||||||
for(int i = 0;i < n;i++){
|
* @returns none
|
||||||
position[(ar[i]/cur_digit)%10]++; // counting frequency of 0-9 at cur_digit.
|
*/
|
||||||
|
void radix :: step_ith(int cur_digit){ // sorting according to current digit.
|
||||||
|
std::vector<int> position(10,0);
|
||||||
|
for(int i = 0;i < n;i++){
|
||||||
|
position[(ar[i]/cur_digit)%10]++; // counting frequency of 0-9 at cur_digit.
|
||||||
|
}
|
||||||
|
int cur = 0;
|
||||||
|
for(int i = 0;i < 10;i++){
|
||||||
|
int a = position[i];
|
||||||
|
position[i] = cur; // assingning starting position of 0-9.
|
||||||
|
cur += a;
|
||||||
|
}
|
||||||
|
std::vector<int> temp(n);
|
||||||
|
for(int i = 0;i < n;i++){
|
||||||
|
temp[position[(ar[i]/cur_digit)%10]] = ar[i]; // storing ar[i] in ar[i]'s cur_digit expected position of this step.
|
||||||
|
position[(ar[i]/cur_digit)%10]++; // incrementing ar[i]'s cur_digit position by 1, as current place used by ar[i].
|
||||||
|
}
|
||||||
|
for(int i = 0;i < n;i++){
|
||||||
|
ar[i] = temp[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
int cur = 0;
|
/**
|
||||||
for(int i = 0;i < 10;i++){
|
* @brief Function to sort vector digit by digit.
|
||||||
int a = position[i];
|
* @returns none
|
||||||
position[i] = cur; // assingning starting position of 0-9.
|
*/
|
||||||
cur += a;
|
void radix :: radix_sort(){
|
||||||
|
int max_ele = get_max();
|
||||||
|
for(int i = 1;max_ele/i > 0;i *= 10){ // loop breaks when i > max_ele because no further digits left to makes changes in array.
|
||||||
|
step_ith(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
int temp[n];
|
} // namespace radix_sort
|
||||||
for(int i = 0;i < n;i++){
|
|
||||||
temp[position[(ar[i]/cur_digit)%10]] = ar[i]; // storing ar[i] in ar[i]'s cur_digit expected position of this step.
|
|
||||||
position[(ar[i]/cur_digit)%10]++; // incrementing ar[i]'s cur_digit position by 1, as current place used by ar[i].
|
|
||||||
}
|
|
||||||
for(int i = 0;i < n;i++){
|
|
||||||
ar[i] = temp[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void radix :: radix_sort(){
|
|
||||||
int max_ele = get_max();
|
|
||||||
for(int i = 1;max_ele/i > 0;i *= 10){ // loop breaks when i > max_ele because no further digits left to makes changes in array.
|
|
||||||
step_ith(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} // namespace radix_sort
|
|
||||||
} // namespace sort
|
} // namespace sort
|
||||||
|
|
||||||
void test1(){
|
/**
|
||||||
std::vector<int> ar = {432,234,143,332,123};
|
* Function to test the above algorithm
|
||||||
sort::radix_sort::radix obj(ar);
|
* @returns none
|
||||||
obj.radix_sort();
|
*/
|
||||||
obj.show();
|
void test1(){
|
||||||
}
|
std::vector<int> ar = {432,234,143,332,123};
|
||||||
void test2(){
|
sort::radix_sort::radix obj(ar);
|
||||||
std::vector<int> ar = {213,3214,123,111,112,142,133,132,32,12,113};
|
obj.radix_sort();
|
||||||
sort::radix_sort::radix obj(ar);
|
obj.show();
|
||||||
obj.radix_sort();
|
}
|
||||||
obj.show();
|
/**
|
||||||
}
|
* Function to test the above algorithm
|
||||||
|
* @returns none
|
||||||
|
*/
|
||||||
|
void test2(){
|
||||||
|
std::vector<int> ar = {213,3214,123,111,112,142,133,132,32,12,113};
|
||||||
|
sort::radix_sort::radix obj(ar);
|
||||||
|
obj.radix_sort();
|
||||||
|
obj.show();
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @brief Main function
|
* @brief Main function
|
||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
@@ -111,5 +135,15 @@ int main()
|
|||||||
{
|
{
|
||||||
test1();
|
test1();
|
||||||
test2();
|
test2();
|
||||||
|
int n;
|
||||||
|
std::cin>>n;
|
||||||
|
std::vector<int> ar;
|
||||||
|
for(int i = 0;i < n;i++){
|
||||||
|
int x;std::cin>>x;
|
||||||
|
ar.push_back(x);
|
||||||
|
}
|
||||||
|
sort::radix_sort::radix obj(ar);
|
||||||
|
obj.radix_sort();
|
||||||
|
obj.show();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user