Solved

Radix Sort

Posted on 2001-09-10
5
306 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

803 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