473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Radix Sorting

Hi everyone. I'm having trouble with this radix sorting program. I've
gotten some of it coded except for the actual sorting :( The book I'm
teaching myself with (Data Structures Using C and C++) just doesn't
explain things good at all and the tutorials I've viewed don't really
explain least significant digit first sorting or show examples, which
would be most helpful. I've commented the section where I know that
the iteration of the least significant digit sorting goes. Any input
is greatly appreciated.

Thanks,
James
[code]
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int check_order(unsigned int *, int);
void lsd_radix_sort(unsigned int *, int);

main(int argc, char *argv[]) {
int i, nvals = 10000;
unsigned int *array;
if(argc > 1)
nvals = atoi(argv[1]);
array = malloc(nvals * sizeof(unsigned int));
for(i = 0; i < nvals; i++)
array[i] = random();
lsd_radix_sort(array, nvals);
if(i = check_order(array, nvals))
printf("%d misorderings\n", i);
else
printf("array is in order\n");
}

int check_order(unsigned int *ip, int n) {
int i, nrev = 0;
for(i = 1; i < n; i++)
if(ip[i-1] > ip[i])
nrev++;
return(nrev);
}

#define BITS 8 /* specify the radix by number of bits */
#define RADIX (1<<BITS)
#define DIGITS (32/BITS)
void lsd_radix_sort(unsigned int *array, int n) {
int i, j, k, d, shift;
int *bin[RADIX], n_in_bin[RADIX], size_bin[RADIX];
unsigned int v;

//least significant digit first radix sort

}

Nov 15 '05 #1
7 10066
On Wed, 26 Oct 2005 10:12:31 -0400, Foodbank <v8********@yahoo.com> wrote:
Hi everyone. I'm having trouble with this radix sorting program. I've
gotten some of it coded except for the actual sorting The book I'm
teaching myself with (Data Structures Using C and C++) just doesn't
explain things good at all and the tutorials I've viewed don't really
explain least significant digit first sorting or show examples, which
would be most helpful.


I am sorry that I don't have more helpful advice to give you, but I did a
Radix sort quite successfully as one of my first takes into Scheme. I
unfortunately only have that as an example of how it might be done. If you
don't mind, you might check that out for some ideas about how the logic
and such might flow, and since the concept is fairly simple, you should be
able to put the code into your own words quite easily.

http://www.aaronhsu.com/progport.html -> Page w/ Description
http://www.aaronhsu.com/src/radix-sort.zip -> Source

The functions are all pretty straightforward, and it will give you a
simple idea of how the sorting is done. From what I understand, a C type
Radix sort is easily and most effeciently executed by just shifting
pointers or something like that. I was thinking of actually doing the C
version of my Radix sort just a little while ago, and I might just do it
now.

- Arctic

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Nov 15 '05 #2
Thanks for the help Arctic, but your files are not really viewable the
way that they are presented. When opened by an editor, the code is one
or two really long horizontal lines. When you zipped it, that must
have not kep the original formatting of the file. If possible, could
you post a text file or similar so that I can actually read and follow
your code?

Thanks,
James

Nov 15 '05 #3
On 2005-10-27, Foodbank <v8********@yahoo.com> wrote:
Thanks for the help Arctic, but your files are not really viewable
the way that they are presented. When opened by an editor, the
code is one or two really long horizontal lines. When you zipped
it, that must have not kep the original formatting of the file.
If possible, could you post a text file or similar so that I can
actually read and follow your code?

Thanks,
James


Most likely you are on windows and he is on a unix-like platform -
select all text and paste into wordpad, or open the file in wordpad
in the first place.
Nov 15 '05 #4
That worked Jordan. Thanks for the advice.

As for the program that you provided Arctic. I appreciate the effort,
but I think that code is a little out of my league. I'm trying to do
the most basic of radix sorting and it was hard for me to understand
that code. If anyone else has any input regarding my code, I'd greatly
appreciate it (beginner radix sorting input that is :) )

James

Nov 15 '05 #5
Foodbank <v8********@yahoo.com> wrote:
As for the program that you provided Arctic. I appreciate the effort,
but I think that code is a little out of my league. I'm trying to do
the most basic of radix sorting and it was hard for me to understand
that code. If anyone else has any input regarding my code, I'd greatly
appreciate it (beginner radix sorting input that is :) )


It is proper Usenet etiquette to include the relevant portions of the text
you are replying to. To do this using Google groups, please follow the
instructions below, penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #6
On Thu, 27 Oct 2005 08:48:41 -0400, Foodbank <v8********@yahoo.com> wrote:
As for the program that you provided Arctic. I appreciate the effort,
but I think that code is a little out of my league. I'm trying to do
the most basic of radix sorting and it was hard for me to understand
that code. If anyone else has any input regarding my code, I'd greatly
appreciate it (beginner radix sorting input that is )


The only file you really ought to concern yourself with then, is the
rdx_algor.scm file. It's less than 50 lines long. I don't think you can
get more simple than that, not by much anyways.

The basic idea is:

Get an unsorted list (it's a linked list in my program) and the number of
digits in each number.

Create bins, the number of which is equal to the base of the numbers you
are sorting, that is, 10 bins for a decimal system, 2 bins for binary, etc.

Read the first number from the list, and the appropriate digit from that
number, and place the digit into the according bin.

Repeat with all the numbers in the list, and then recombin the elements
into a list starting with the lowest bin (0) if you are doing lowest to
highest, or vice versa if you are doing highest to lowest.

Repeat this with each digit in the numbers.

My program is one main recursive loop, and one other loop. Two loops in
total:

main : <unsorted list> <digits> -> <list>

The way the main loop works is by decrementing the digit (like a counter)
until it is zero, at which point it returns the fully sorted list. The
reason this happens is because the divide function uses this digit to
reference a point in the number. So if we had ten digits in each number,
the first call to divide would have 10 as the value for digits. And
calling the 10th element of a string would be the least significant digit
if the string were a number. It's a pretty simple recursive loop.

All the recombine function does is take a two dimensional array of size 10
by whatever (linked lists) and append all the lists together into one.

All the divide function does, really, is loop through the list, and look
at the digit referenced by its second argument, and append that number to
the appropriate bin list. That's the hardest loop, and the logic might go
something like:

divide : <list> <digit> -> <2d array>

create an array of 10 digits, each element being a list of numbers (2d
array)
read the first item from list
read the element referenced by <digit>
switch (element)
case 0: append array[0] with num
case 1: append array[1] with num
case 2: etc.
etc.
repeat procedure on next item until no more items in list
return the 2d array to be recombined by another function.

So really the logic isn't too hard. Perhaps what is throwing you is that I
used recursion and functional programming style, which doesn't always map
directly to C. BUt what fun would it be if I gave you a working C program
of the same? I am sure you can find some of those around, but I doubt
that's what you want to do. This is a learning exercise, no?

Really, a radix sort is a radix sort, the logic is all the same.
Understand that there are really only two logical loops happening. One is
controlling which digit of the numbers you are working on, the other is
reading that digit and putting the numbers into the appropriate slots.
There are other ways to do it, I am sure, but I find that easiest.

I hope that helps a bit.

- Arctic

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Nov 15 '05 #7
I really appreciate your help Arctic. As for a working program, this
is a learning exercise, but I think I would be so much better off with
something that I could run and follow the steps through with printfs.
If you don't mind, could you possibly post an example that you have?

Thanks,
James

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
18
by: Kevin Doyle | last post by:
Hi I just found this macro. I want to convert a dword to string using this _ultoa but I don't understand the radix part of the function. what value of radix should I use so as not to effect the...
2
by: Foodbank | last post by:
Hi everyone, I'm having trouble implementing a supposedly simple Least Significant Digit first Radix Sort. The book I'm using gave me somewhat of a template that I touched up (below), but I'm...
1
by: Foodbank | last post by:
Hi, I'm currently teaching myself C data structures in preparation for a job transition and I've just passed the point in which radix sorting was covered in my book. I've recently finished a...
3
by: Tracey | last post by:
I'm learning C++ and was stuck when writing code to convert a negative decimal number string to a hexdecimal number. For example, suppose the function interface is defined as: int atoi( const...
1
by: crimson08 | last post by:
does anyone know how to use radix sorting in a linked list???
0
by: Nick Keighley | last post by:
On Oct 25, 1:50 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote: since the pointer indicates the "last" digit. It indicates how many digits there are. I'm guessing that N is just some large...
8
by: tejas2991 | last post by:
could someone please gimme the main code for "radix sort" using stack
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.