aspose file tools*
The moose likes Beginning Java and the fly likes Inserting numbers in order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inserting numbers in order" Watch "Inserting numbers in order" New topic
Author

Inserting numbers in order

Vincent Mule
Greenhorn

Joined: Oct 04, 2012
Posts: 14
I was wondering, is there way to take numbers from an array and insert them in order but without doing a bubble sort? What would some of this code look like?
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

Are you talking about Insertion sort?
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

Maybe put it into a List and use the sort method?


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Vincent Mule
Greenhorn

Joined: Oct 04, 2012
Posts: 14
Well my professor wants us to take numbers from args[0] and insert them in order.

My current code looks like this

and the insertInOrder method

Vincent Mule
Greenhorn

Joined: Oct 04, 2012
Posts: 14
Whenever I run this, all it returns is 0s, not the numbers from the array.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Vincent Mule wrote:Whenever I run this, all it returns is 0s, not the numbers from the array.

Well, your insertInOrder() method looks way over-engineered to me.

My advice: STOP CODING and think about what you need to do.

Get out lots of paper, a pencil, and an eraser, and do LOTS of scribbling.

Write out some scenarios:
After you've added the 2nd number to your array, is it sorted? What about after you add the 3rd? And the 4th? Is it still sorted? How does that help you?

What if you already have 5 numbers in your array and you're trying to add a 6th? What do you have to do? - exactly.

Write down ALL the steps you need to go through in English before you write another line of code.

Programming is about thinking, not coding; and you'll never be able to solve a problem in Java until you understand it in English.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting numbers in order