• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there a better way to do it?

 
Ranch Hand
Posts: 91
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 >
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 91
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..?

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply..
 
Vineeth Menon
Ranch Hand
Posts: 91
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 91
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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???

>
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic