This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting numbers in order

 
Vincent Mule
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about Insertion sort?
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe put it into a List and use the sort method?
 
Vincent Mule
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever I run this, all it returns is 0s, not the numbers from the array.
 
Winston Gutkowski
Bartender
Pie
Posts: 10490
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic