954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

radix sort

can anyone give me a example of a radix sort

ching
Newbie Poster
6 posts since Jun 2004
Reputation Points: 11
Solved Threads: 0
 

Hello,
I have done the Radix with queue
here you are an example
But the queue is my own Queue

retrieve: return the first element
serve : delete the first element;
dequeue: insertlast

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
#include <ctime>
#include "queue.h"
#define MAX 11
void CopyToLast(Queue<int> []);
int main()
{
	Queue<int> q[MAX];
	srand(time(0));
	int res,i,j,digit = 4,passHelper=10,temp,pos;
	cout << "Before Sorting : \n";
	for(i=0;i<100;i++) {
		res = rand() % 10000;
		cout << res << "|";		
		q[MAX-1].append(res);
		
	}
		
	for(i=0;i<digit;i++) {
		
		while(!q[MAX-1].empty()) {
			q[MAX-1].retrieve(temp);
			q[MAX-1].serve();
			pos=temp;
			for(j=0;j<i;j++)
				pos/=10;
			pos %= 10;
			q[pos].append(temp);
		}
		
		
		passHelper *= 10;
		CopyToLast(q);
	}
	
	cout << "\nAfter sorting :\n";
	while(!q[MAX-1].empty()) {
		q[MAX-1].retrieve(temp);
		cout << temp << "|";
		q[MAX-1].serve();
	}
	
	return 0;
}
void CopyToLast(Queue<int> g[])
{
	int i,temp;
	for(i=0;i<MAX-1;i++) {
		while(!g[i].empty()) {
			g[i].retrieve(temp);
			g[MAX-1].append(temp);
			g[i].serve();
		}
	}

}

If you want here you are my own queue class

Attachments Queue.h (2.64KB)
abu_sager
Newbie Poster
10 posts since Jun 2004
Reputation Points: 12
Solved Threads: 2
 

This program is generates 100 random integers and store them in the latest Queue then start sorting

abu_sager
Newbie Poster
10 posts since Jun 2004
Reputation Points: 12
Solved Threads: 2
 
can anyone give me a example of a radix sort

Here's an explanation to complement Abu's code example:

A radix sort works with the digits of a number, working from the least significant digit to the most. Here's a sample set of numbers:[indent]423 586 403 171 92 536 234 243[/indent]
First, sort the numbers by the least siginificant digit (ones). For this algorithm to work, your buckets have to be order-preserving.[indent]171 92 423 403 243 234 586 536[/indent]
Then, sort them by the next most significant digit (tens):[indent]403 423 234 536 243 171 586 92[/indent]
Now hundreds:[indent]92 171 234 243 403 423 536 586[/indent]
...and they're sorted. Each step doesn't have to be a bucket sort; any order-preserving sort will work.

gusano79
Posting Pro
521 posts since May 2004
Reputation Points: 182
Solved Threads: 77
 

Can anyone provide me the Radix sorting through other than the Queue.w8ng for the replyyyyyy......

Champhero
Newbie Poster
2 posts since Oct 2009
Reputation Points: 9
Solved Threads: 0
 
Can anyone provide me the Radix sorting through other than the Queue.w8ng for the replyyyyyy......


You can use buckets to code radix sort.

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

dibilu

dibilasd
Newbie Poster
2 posts since Nov 2009
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You