aspose file tools*
The moose likes Beginning Java and the fly likes insertion sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "insertion sort" Watch "insertion sort" New topic
Author

insertion sort

Alex Mokosh
Greenhorn

Joined: Feb 06, 2012
Posts: 4
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???
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: Feb 06, 2012
Posts: 4
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

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: Feb 06, 2012
Posts: 4
I just can not figure it out.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 06, 2012
Posts: 4
I got it now, that works, thanks for your help
Alexandre Mendes
Greenhorn

Joined: Sep 16, 2011
Posts: 17

Alexandre Mendes
Greenhorn

Joined: Sep 16, 2011
Posts: 17
ok
Alexandre Mendes
Greenhorn

Joined: Sep 16, 2011
Posts: 17
look at the code. It can Help.


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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.
 
Don't get me started about those stupid light bulbs.
 
subject: insertion sort