• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

insertion sort

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm a new here and I need some help to solve this problem.
I need to sort String alphabetically by using insertion sort, so that's what I've got so far:



So, any ideas, what's wrong in my code???
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alex. Welcome to The Ranch!

I've no idea if your algorithm is OK, but you're getting that output because you're printing out a character array. Arrays don't automatically print out in a helpful way in Java - what you're seeing is the result of the default implementation of toString() in Object.

Since it's a char array, there's an easy way to get it back into a String, though:
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Out of interest, why are you declaring ch and res at the class level? They're only needed within the method, so your code would be more readable and maintainable if you declared them when they are used.
 
Alex Mokosh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed it,thanks, now I got some normal output bcdb, but it's wrong, should be abcd, so can you find some error in my code??
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your line 14 supposed to swap two values? Because at the moment it overwrites ch[j] with ch[j - 1], which means that whatever you had in the array at ch[j] is lost. That's where the 'a' has gone.

Incidentally, the way I did that was to step through the code line-by-line, writing down the value of all the variables at each step. If you've got access to a debugger it's even easier - that will step through the code for you and you can check the value of variables whenever you like.
 
Alex Mokosh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just can not figure it out.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, what is your line 14 supposed to be doing? At the moment it would take an array {ba}, and that step will turn it into {bb}. That's not what you intended, is it?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Mokosh wrote: I just can not figure it out.



Can't figure what out? The swapping part that Matthew is talking about? Imagine that you have two ping-pong balls--one red and one green. Now imagine that you have some cups, each just big enough to hold one ping-pong ball. You put the red ball in the cup labeled 'A' and the green ball in the cup labeled 'B'. Now you want to switch them, so red is in B and green is in A. Each ball always has to be in a cup, or it will roll away and you'll lose it. How would you do that?
 
Alex Mokosh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now, that works, thanks for your help
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Alexandre Mendes
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
 
Alexandre Mendes
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at the code. It can Help.


 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've edited your most recent post to use code tags. This preserves formatting, does context highlighting, and makes it much easier to read.

Next time, after pasting in your code, just highlight it all and click the 'code' button, just like you were trying to make it italic or bold.

Thanks!!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexandre Mendes wrote:look at the code. It can Help.



Please don't do that. This site is NotACodeMill.(⇐click) Just handing somebody the solution doesn't help him learn. And, as it clearly states on the topics page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."

Thanks, and welcome to the Ranch!
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexandre Mendes wrote:look at the code. It can Help.

Not any more. In view of what Jeff Verdegan has just told you, I have gone back and edited your post. Please remember people learn less by being given a complete answer like that. Your code would not have helped at all, I am afraid.

Jeff Verdegan wrote:
Please don't do that. This site is NotACodeMill.(⇐click) Just handing somebody the solution doesn't help him learn. And, as it clearly states on the topics page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers." . . .

Thank you for noticing.
 
Campbell Ritchie
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alexandre Mendes,
Your post was moved to a new topic.

Please continue discussion of the sorting algorithm here.
Please continue any discussion of what sort of answers ought to be provided in the new post.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic