Solved

Radix Sort

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

837 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