• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Inserting numbers in order

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you talking about Insertion sort?
 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever I run this, all it returns is 0s, not the numbers from the array.
 
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

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
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic