Need Help: Converting int[] based Radix Sort to string[] based Radix Sort [modified]
-
Hello. I got a code here in codeproject.com about radix sorting but it only sorts int based data. I want to use it to sort a string based data. My first try is I got first the ASCII codes of each characters in the strings then passed it in the radix sort code but what it gives me is totally different. This is the link of the code that I got: click here Here is the output after getting the ASCII codes and pass it to the radix sort:
SHOW DATABASE: SORTING BY LAST NAME
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
97
97
97
110
110
111
111
111
114
115
115
115
117
122
122
What should I do for it to be able to sort string?
modified on Friday, September 4, 2009 6:02 AM
-
Hello. I got a code here in codeproject.com about radix sorting but it only sorts int based data. I want to use it to sort a string based data. My first try is I got first the ASCII codes of each characters in the strings then passed it in the radix sort code but what it gives me is totally different. This is the link of the code that I got: click here Here is the output after getting the ASCII codes and pass it to the radix sort:
SHOW DATABASE: SORTING BY LAST NAME
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
97
97
97
110
110
111
111
111
114
115
115
115
117
122
122
What should I do for it to be able to sort string?
modified on Friday, September 4, 2009 6:02 AM
-
Hello. I got a code here in codeproject.com about radix sorting but it only sorts int based data. I want to use it to sort a string based data. My first try is I got first the ASCII codes of each characters in the strings then passed it in the radix sort code but what it gives me is totally different. This is the link of the code that I got: click here Here is the output after getting the ASCII codes and pass it to the radix sort:
SHOW DATABASE: SORTING BY LAST NAME
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
97
97
97
110
110
111
111
111
114
115
115
115
117
122
122
What should I do for it to be able to sort string?
modified on Friday, September 4, 2009 6:02 AM
Hi, I don't know why you insist on using radix sort, as it will be slower than a regular sort when sorting strings. Here is how it could work: - determine the length L of the longest string; - treat all strings as having length L by virtually appending NULL characters; - the Nth "digit" of a string has a value that equals the (int) value of its Nth character (where you use zero if N exceeds the string length). So sort all strings according to their (L-1)th character then sort according to their (L-2)th character etc and finally sort according to their first character As I said, it will be slow, as you have to get the characters, one by one, and compare those, whereas a normal sort would be based on a comparer method (such as string.Compare) that takes the whole string into account right away. :)
Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig: