943,510 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 28286
  • C++ RSS
Jun 8th, 2004
1

radix sort

Expand Post »
can anyone give me a example of a radix sort
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ching is offline Offline
6 posts
since Jun 2004
Jun 9th, 2004
1

Re: radix sort

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include "queue.h"
  8. #define MAX 11
  9. void CopyToLast(Queue<int> []);
  10. int main()
  11. {
  12. Queue<int> q[MAX];
  13. srand(time(0));
  14. int res,i,j,digit = 4,passHelper=10,temp,pos;
  15. cout << "Before Sorting : \n";
  16. for(i=0;i<100;i++) {
  17. res = rand() % 10000;
  18. cout << res << "|";
  19. q[MAX-1].append(res);
  20.  
  21. }
  22.  
  23. for(i=0;i<digit;i++) {
  24.  
  25. while(!q[MAX-1].empty()) {
  26. q[MAX-1].retrieve(temp);
  27. q[MAX-1].serve();
  28. pos=temp;
  29. for(j=0;j<i;j++)
  30. pos/=10;
  31. pos %= 10;
  32. q[pos].append(temp);
  33. }
  34.  
  35.  
  36. passHelper *= 10;
  37. CopyToLast(q);
  38. }
  39.  
  40. cout << "\nAfter sorting :\n";
  41. while(!q[MAX-1].empty()) {
  42. q[MAX-1].retrieve(temp);
  43. cout << temp << "|";
  44. q[MAX-1].serve();
  45. }
  46.  
  47. return 0;
  48. }
  49. void CopyToLast(Queue<int> g[])
  50. {
  51. int i,temp;
  52. for(i=0;i<MAX-1;i++) {
  53. while(!g[i].empty()) {
  54. g[i].retrieve(temp);
  55. g[MAX-1].append(temp);
  56. g[i].serve();
  57. }
  58. }
  59.  
  60. }
If you want here you are my own queue class
Attached Files
File Type: h Queue.h (2.6 KB, 388 views)
Reputation Points: 12
Solved Threads: 2
Newbie Poster
abu_sager is offline Offline
10 posts
since Jun 2004
Jun 9th, 2004
0

Re: radix sort

This program is generates 100 random integers and store them in the latest Queue then start sorting
Reputation Points: 12
Solved Threads: 2
Newbie Poster
abu_sager is offline Offline
10 posts
since Jun 2004
Jun 10th, 2004
1

Re: radix sort

Quote originally posted by ching ...
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:
423 586 403 171 92 536 234 243
First, sort the numbers by the least siginificant digit (ones). For this algorithm to work, your buckets have to be order-preserving.
171 92 423 403 243 234 586 536
Then, sort them by the next most significant digit (tens):
403 423 234 536 243 171 586 92
Now hundreds:
92 171 234 243 403 423 536 586
...and they're sorted. Each step doesn't have to be a bucket sort; any order-preserving sort will work.
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004
Oct 30th, 2009
0
Re: radix sort
Can anyone provide me the Radix sorting through other than the Queue.w8ng for the replyyyyyy......
Reputation Points: 9
Solved Threads: 0
Newbie Poster
Champhero is offline Offline
2 posts
since Oct 2009
Oct 30th, 2009
0
Re: radix sort
Click to Expand / Collapse  Quote originally posted by Champhero ...
Can anyone provide me the Radix sorting through other than the Queue.w8ng for the replyyyyyy......
You can use buckets to code radix sort.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Nov 2nd, 2009
0
Re: radix sort
dibilu
Reputation Points: 8
Solved Threads: 0
Newbie Poster
dibilasd is offline Offline
2 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compile pthreads in windows netbeans
Next Thread in C++ Forum Timeline: Radix Sort Code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC