aspose file tools*
The moose likes Beginning Java and the fly likes Is there a better way to do it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there a better way to do it?" Watch "Is there a better way to do it?" New topic
Author

Is there a better way to do it?

Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Howdy Folks,

I have made a small program to sort a character string alphabetically by using ASCII values. The code works fine, there are no problem in it. What I want to know is that is there is a better way of doing this??? I'm sure there is...if you guys could give me some tips that would be great... here is the code...



Thanks >



VM
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's a much better way. Here's what I would do:

(1) Convert the String to an array of chars.

(2) Sort that array. (Hint: Don't write your own code to sort arrays.)

(3) Convert the sorted array back to a String.
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Hey,
Thanks for the response mate. I guess you are mentioning the java.util.Arrays.sort way right? I am trying to sort without using any inbuilt functions.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Vineeth Menon wrote:Hey,
Thanks for the response mate. I guess you are mentioning the java.util.Arrays.sort way right? I am trying to sort without using any inbuilt functions.


Then you have to work out some algorithm which sorts the characters. Internally, different versions of Arrays.sort() use different algorithms such as merge-sort /quick-sort etc.


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7647
    
  19

Vineeth Menon wrote:Thanks for the response mate. I guess you are mentioning the java.util.Arrays.sort way right? I am trying to sort without using any inbuilt functions.

Fair enough, but what Paul said still applies: convert the String to characters, not numbers; ie, convert it to a List<Character> and then sort.

Why? Because Characters compare lexically; integers compare by value - at least, they do if you use compareTo(), which is what you should be using.

Beyond that, the only thing I can suggest is to either use List methods directly or use arrays. Right now, you're converting a String to a List, then a List to an array to sort it, then...well, to be honest, I'm not quite sure what you are doing with the result; you appear to be creating a single character.

Also, if this is simply an exercise in writing a sort, concentrate on that. Forget about Strings and converting and start out with a List (or array) of Characters, and write a method to sort that. Once you've got it working, then is the time to worry about converting it to and from Strings.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

Mansukhdeep Thind wrote:
Vineeth Menon wrote:
Then you have to work out some algorithm which sorts the characters. Internally, different versions of Arrays.sort() use different algorithms such as merge-sort /quick-sort etc.


What is the sorting method used in Arrays.sort() and Collections.sort() methods..?


Ramesh-X
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7647
    
  19

Ramesh Pramuditha Rathnayake wrote:What is the sorting method used in Arrays.sort() and Collections.sort() methods..?

I suggest you look at the docs, because they explain exactly what sort of sort is used, and also its characteristics.

Winston
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

Thanks for the reply..
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Winston Gutkowski wrote:

Beyond that, the only thing I can suggest is to either use List methods directly or use arrays. Right now, you're converting a String to a List, then a List to an array to sort it, then...well, to be honest, I'm not quite sure what you are doing with the result; you appear to be creating a single character.

Also, if this is simply an exercise in writing a sort, concentrate on that. Forget about Strings and converting and start out with a List (or array) of Characters, and write a method to sort that. Once you've got it working, then is the time to worry about converting it to and from Strings.

Winston


Hey Winston,

Thanks for the response mate. To start of with I started this program as an exercise, and if possible create my own sort function similar to java.util.Arrays.sort . So the best thing that came to my mind was to sort the character String by using the ASCII value. It did get a bit dodgy around the edges as well (converting from String to List and to an array) I am working on that though. As for the sorting, I was thinking to implement a QuickSort... but my ultimate question is would sorting be done best by ASCII value or is there are better way that using ASCII (sorry if was a dumb question )
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7647
    
  19

Vineeth Menon wrote:I was thinking to implement a QuickSort... but my ultimate question is would sorting be done best by ASCII value or is there are better way that using ASCII (sorry if was a dumb question )

I think I already answered that. Sorting by ASCII value only works for plain ascii characters (ie, 'aA' through 'zZ'). If you have any accented characters or other anomalies, they will probably screw up your ordering; but the Character class deals with a lot of that stuff for you (at least, I assume it does; and if it doesn't, there are also Text classes around).

So, back to my point: If you want to implement a Quicksort, concentrate on that and get it working before you think about anything else.

And I hate to say, but whatever you come up with is still likely to be less efficient than Java's own sort() methods, which (as I recall) use a combination of selection sort and mergesort, based on well-tested thresholds.

So, by all means do the exercise - they're extremely informative - but don't expect a "better mousetrap" at the end.

Winston
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Winston Gutkowski wrote:

So, back to my point: If you want to implement a Quicksort, concentrate on that and get it working before you think about anything else.


Winston,

I got the Quicksort working. Had to spend some time on it. but got it working in the end. Yeah and you're right, there is no way my sort would be better that Java. As said before I just wanted to make a sorting function on my own hoping I'll learn something new along the way

PS: Here is the code by the way, if it's not too much trouble could you have a look at it and give me some inputs???

>
 
 
subject: Is there a better way to do it?