Please implement LSD Radix Sort by C++ code.
Fill in the blanks marked "//TODO"
Template:
Hint :
Assume that all input parameters would be valid.
Implement the functions below:
radix_sort() : The function that implements the main radix sort function
digits() : Utility function to get the number of digits in the max_element
You don't have to use template code (you can use it or not), as long as you can output the correct result
The numbers to be sorted will be integers and will not repeat
Output each number in each bin of each round of radix sort
The number stored in each bin will be connected with a singly linked list
Because the representation of the given number is decimal, a total of ten bins are required
You can use “int max_val = *max_element(arr.begin(), arr.end());” to get the maximum number in the array.
You can use the following code to output the numbers in all bins in a round :
10 . The only four libraries that can be included are the following :
#include<iostream>
#include<vector>
#include <cmath>
#include <algorithm>
11. You can declare and initialize the bucket to be used for sort in the following way :
12. You can directly take the value of the input array in the following way :
arr[j] (j is the index of the array)
1.First, you have to input the number of integers you want to sort
2.Second, you have to enter the sequence to be sorted
3.Suppose the number sequence we want to sort is 11, 5, 8, 3, 7
First, you have to input 5 (because the number of integers is 5)
Second, you have to input 11 5 8 3 7(the sequence we want to sort)
So, the actual input format is as follows:
5
11 5 8 3 7
1.Output each number in each bin of each round of radix sort
2.Taking the above Input example as an example, the results shown below will be output :
0 NULL
1 11
2 NULL
3 3
4 NULL
5 5
6 NULL
7 7
8 8
9 NULL
0 3 5 7 8
1 11
2 NULL
3 NULL
4 NULL
5 NULL
6 NULL
7 NULL
8 NULL
9 NULL
3 5 7 8 11
Explanation :
(For the output format, please refer to printlist function and Hint 9.)
Because the largest number is two digits, only two rounds will be played
0 NULL
1 11
2 NULL
3 3
4 NULL
5 5
6 NULL
7 7
8 8
9 NULL
---------------The above is the number stored in the bin from 0 to 9 in the first round-----------
0 3 5 7 8
1 11
2 NULL
3 NULL
4 NULL
5 NULL
6 NULL
7 NULL
8 NULL
9 NULL
-------------The above is the number stored in the bin from 0 to 9 in the second round-----------
3 5 7 8 11 ------> The final result sorted by LSD Radix Sort