Solved

Radix Sort

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

758 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