Question

#ifndef RADIXSORT #define RADIXSORT #include<vector> // Function to get maximum value in array a[]. template <typename...

#ifndef RADIXSORT
#define RADIXSORT

#include<vector>

// Function to get maximum value in array a[].
template <typename dataType>
int getmax(dataType a[], int n)
{
int max = a[0];
for (int x = 1; x < n; x++)
if (a[x] > max)
max = a[x];
return max;
}

// Function to do counting sort according to significant digits repesented by
// exp (where exp is 10^i).
template <typename dataType>
void CountSort(dataType a[], int n, int exp)
{
vector <int> result(n);
int i, count[10] = { 0 };

// Counting occurence of digits
for (i = 0; i < n; i++)
count[(a[i] / exp) % 10]++;

// Changing the position of count so that it appears at actual position in result.
for (i = 1; i < 10; i++)
count[i] += count[i - 1];

// Resultant output array
for (i = n - 1; i >= 0; i--)
{
result[count[(a[i] / exp) % 10] - 1] = a[i];
count[(a[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
a[i] = result[i];
}

// Radix Sort to sort a[] of given size.
template <typename dataType>
void radixSort(dataType a[], int n)
{
int exp, i;
i = getmax(a, n);
for (exp = 1; i / exp > 0; exp *= 10)
CountSort(a, n, exp);
}
#endif // RADIXSORT

I used the vector on vector <int> result(n). Is there anything that I can change within this code to make sure it works properly. C++ only.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts/errors, please give me comment...

#ifndef RADIXSORT

#define RADIXSORT

#include <vector>

// Function to get maximum value in array a[].

template <typename dataType>

int getmax(dataType a[], int n)

{

    int max = a[0];

    for (int x = 1; x < n; x++)

        if (a[x] > max)

            max = a[x];

    return max;

}

// Function to do counting sort according to significant digits repesented by

// exp (where exp is 10^i).

template <typename dataType>

void CountSort(dataType a[], int n, int exp)

{

    std::vector<dataType> result(n);

    int i, count[10] = {0};

    // Counting occurence of digits

    for (i = 0; i < n; i++)

        count[(a[i] / exp) % 10]++;

    // Changing the position of count so that it appears at actual position in result.

    for (i = 1; i < 10; i++)

        count[i] += count[i - 1];

    // Resultant output array

    for (i = n - 1; i >= 0; i--)

    {

        result[count[(a[i] / exp) % 10] - 1] = a[i];

        count[(a[i] / exp) % 10]--;

    }

    for (i = 0; i < n; i++)

        a[i] = result[i];

}

// Radix Sort to sort a[] of given size.

template <typename dataType>

void radixSort(dataType a[], int n)

{

    int exp, i;

    i = getmax(a, n);

    for (exp = 1; i / exp > 0; exp *= 10)

        CountSort(a, n, exp);

}

#endif // RADIXSORT

main.cpp

#include<iostream>

#include<vector>

#include "radix_sort.h"

using namespace std;

int main(){

    int arr[] = {27,12,72,36,37,29,26,22,97,86};

    radixSort(arr, 10);

    for(int i=0; i<10; i++){

        cout<<arr[i]<<endl;

    }

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
#ifndef RADIXSORT #define RADIXSORT #include<vector> // Function to get maximum value in array a[]. template <typename...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • C++ There is a premade heap.h file. You will write a program that creates a heap,...

    C++ There is a premade heap.h file. You will write a program that creates a heap, and then asks the user to enter values which you will put in the heap (at least 8), and then display the heap Then your program will display a menu with the following choices, and perform the appropriate actions Display the heap Add an item to the heap Remove the largest item Exit The program will continue until the user chooses to exit, at...

  • Need help with the trickle down This is the maxheap.h #ifndef MAXHEAP_H_INCLUDED #define MAXHEAP_H_INCLUDED #include <vector>...

    Need help with the trickle down This is the maxheap.h #ifndef MAXHEAP_H_INCLUDED #define MAXHEAP_H_INCLUDED #include <vector> #include <sstream> #include <string> #include <queue> #include <cmath> // pow() using namespace std; #define PARENT(i) ((i-1) / 2) #define LINE_WIDTH 60.0 template<class T> class MaxHeap{ // private: vector<T> heap; void bubbleUp(int id);    void Heapify() { int length = heap.size(); for(int i=length / 2 -1; i>=0; --i) trickleDown(i); }; public: MaxHeap( vector<T> &vector ) : heap(vector) { Heapify(); } ; MaxHeap() {}; void trickleDown(int...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO::...

    #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO:: default constructor perform new to reserve memory of size INITIAL_CAPACITY. set num_items = 0; */ heap() { // write your code here }; /** Create a heap from a given array */ heap(dtype *other, size_t len) { // write your code here }; /** copy constructor copy another heap to this heap. */ heap(const heap<dtype> &other) { //write your code here }; /** Accessor...

  • //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...

    //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;        int size;        int capacity;               void resize(){            capacity*=2;            T * newArr = new T[capacity];            for(int i=0;i<size;i++){                newArr[i] = arr[i];            }            delete(arr);            arr = newArr;                   }           public:       ...

  • PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

    PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...

  • C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream>...

    C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream> template <typename T, int N=10> class Array { private:   T array[N+1]; public:   Array() {     for(int i=0; i<N+1; i++) array[i] = 0;   }      T& operator [] (int index) {     if (index>N || index < 0)       return array[N];     else       return array[index];   }   template <typename S>   Array<T,N>& operator= (S &other) {     for(int i=0; i<N+1; i++)       array[i] = other[i];         return *this;   } }; template <typename T, typename...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT