13327 - RSTDS110 LSD Radix Sort   

Description

Please implement LSD Radix Sort by C++ code.

 

Fill in the blanks marked "//TODO"

Template:

// implementation of LSD Radix Sort
#include <vector>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
 
// structure for a single linked list to help further in the
// sorting
struct node {
    int data;
    node* next;
};
 
// function for creating a new node in the linked list
struct node* create(int x)
{
    node* temp = new node();
    temp->data = x;
    temp->next = NULL;
 
    return temp;
}
 
// utility function to append node in the linked list
// here head is passed by reference, to know more about this
// search pass by reference
void insert(node*& head, int n)
{
    if (head == NULL) {
        head = create(n);
        return;
    }
 
    node* t = head;
    while (t->next != NULL)
        t = t->next;
    t->next = create(n);
}
 
//The utility function to sequentially output the data of the nodes on the link list from the beginning to the end
void printlist(node* head){
    if (head == NULL){
        cout<<"NULL"<<endl;
        return ;
    }
    node* temp = head;
    do {
        cout<<temp->data<<" ";
        temp = temp->next;
    } while (temp != NULL);
    cout<<endl;
}
 
// utility function to pop an element from front in the list
// for the sake of stability in sorting
int del(node*& head)
{
    if (head == NULL)
        return 0;
    node* temp = head;
    // storing the value of head before updating
    int val = head->data;
 
    // updation of head to next node
    head = head->next;
 
    delete temp;
    return val;
}
 
// utility function to get the number of digits in the
// max_element
int digits(int n)
{
    //TODO
}
 
void radix_sort(vector<int>& arr)
{
    //TODO
}
 
// a utility function to print the sorted array
void print(vector<int> arr)
{
    for (int i = 0; i < arr.size(); i++)
    cout << arr[i] << " ";
    cout << endl;
}
 
int main()
{
    vector<int> arr;
 
    int a;
    int b;
    cin>>a;
    for (int i = 0;i<a;i++){
        cin>>b;
        arr.push_back(b);
    }
    // function call
    radix_sort(arr);
    print(arr);
 
    return 0;
}
 

Hint :

  1. Assume that all input parameters would be valid.

  2. Implement the functions below:

    1. radix_sort() : The function that implements the main radix sort function

    2. digits() : Utility function to get the number of digits in the max_element

  3. You don't have to use template code (you can use it or not), as long as you can output the correct result

  4. The numbers to be sorted will be integers and will not repeat

  5. Output each number in each bin of each round of radix sort

  6. The number stored in each bin will be connected with a singly linked list

  7. Because the representation of the given number is decimal, a total of ten bins are required

  8. You can use “int max_val = *max_element(arr.begin(), arr.end());” to get the maximum number in the array.

  9. You can use the following code to output the numbers in all bins in a round : 

for (int k = 0;k<10;k++){
    cout<<k<<" ";
    printlist(your_bin_array_name[k]);
}

10 . The only four libraries that can be included are the following :

  1. #include<iostream>

  2. #include<vector>

  3. #include <cmath>

  4. #include <algorithm>

​11. You can declare and initialize the bucket to be used for sort in the following way :

// creating buckets to store the pointers
node** bins;
 
// array of pointers to linked list of size 10 as
// integers are decimal numbers so they can hold numbers
// from 0-9 only, that's why size of 10
 
bins = new node*[10];
 
// initializing the hash array with null to all
for (int i = 0; i < 10; i++)
bins[i] = NULL;

12. You can directly take the value of the input array in the following way :

arr[j]  (j is the index of the array)

 

Input

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

 

Output

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

 

Sample Input  Download

Sample Output  Download

Tags




Discuss