Solved

Radix Sort

Posted on 2001-09-10
5
304 Views
Last Modified: 2008-02-01
template <class Item>
Could someone tell me what the variable bytesword is? Can someone tell me what "static Item aux[maxN]" is used for? I want to implement this in my program but I can't use a template. In my class this line would be public:
"void radixLSD(Item a[], int l, int r)" but what would be my private variables? Would I declare Item[maxN] as int Item[maxN]? Are these just generic variables in the template?


template <class Item>
void radixLSD(Item a[], int l, int r)
  { static Item aux[maxN];
    for (int d = bytesword-1; d >= 0; d--)
      {
        int i, j, count[R+1];
        for (j = 0; j < R; j++) count[j] = 0;
        for (i = l; i <= r; i++)
          count[digit(a[i], d) + 1]++;
        for (j = 1; j < R; j++)
          count[j] += count[j-1];
        for (i = l; i <= r; i++)
          aux[count[digit(a[i], d)]++] = a[i];
        for (i = l; i <= r; i++) a[i] = aux[i-l];
      }
  }
0
Comment
Question by:awjackin35
  • 3
5 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6474047
First off, this is not a template class, it is a template function.  So no public/private etc.  bytesword is not defined anywhere in this snippet, so I can't answer that question - neither is R.  

When you call this function, you'd do it like
radixLSD<int> (your parameters)
passing in the type of Item.  The idea is that the code can be used for other data types, so in effect the line in question would become
static int aux[maxN]
you wouldn't have to declare it yourself - that's what this code is for.  

Have a look at the place you got this code from - they should have documented it a bit.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6474071
>>Would I declare Item[maxN] as int Item[maxN]?

Just as I posted that, I noticed the parameter Item a[].  That is the thing to be sorted, so if you called the function as radixLSD<int>, that would have to be defined as an array of ints.

int sortThisPlease[50];
//fill the array here
radixLSD<int>(sortThisPlease, 50, 50);

The code seems a bit strange though - I would suggest testing with a raw function that uses ints, rather than templating one.  When that works, you can do the clever stuff :-)
0
 

Author Comment

by:awjackin35
ID: 6474404
This is from Algorithms in C++ by Sedgewick. I really can't understand the explanation in the book but he calls this a pure radix sort.
0
 
LVL 4

Accepted Solution

by:
IainHere earned 40 total points
ID: 6474435
The example in isn't templeted though, is it?  Try using the original version.  Do you understand both the logic of the approach and the way that his code goes about using it.  Explanations of radix sorting abound:

http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page518.html#45625

Get the raw version from
http://www.ime.usp.br/~yoshi/mac324/Sedgewick/chap10.txt
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6484126
I *think* it's 8 (eight)
If you reverse the for loop in this example...
http://phoenix.liu.edu/~mdevi/algo/RadixSort.htm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now