• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Insert in order  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a list of random numbers and have them inserted in order, heres the function which is supposed to do the task. It's not quite working however. Basically I have a temporary buffer that copies all numbers from the end of the list until it gets to the point where the number is greater than the input_item. As for inserting the item however, it loops through the current list until it finds the position it should be in. Once this is complete what I want to happen is to go from the position the input_item has been placed and put all the numbers in the temporary buffer back in the primary buffer.
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Welcome to JavaRanch!
If you're doing this just to get the job done, then why not use the static java.util.Arrays.sort() functions to sort the complete array. It will likely be faster than anything else you will come up with.
If you're writing your own sort routine just to learn how to write sort routines, then an algorithms book like Sedgewick Volume 1 would be a big help. What you're trying to write is something like an "insertion sort," and it's not only hard to write but terribly slow for arrays of any size; there are better choices.
 
Paul D Statham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well basically i've been given the following pseudo code which I have to expand upon.
 
lowercase baba
Posts: 12745
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul D Statham:

Once this is complete what I want to happen is to go from the position the input_item has been placed...
<snip>
loop2: shift remaining list to its right, start from the end of the list


forgive me, but these seem to be at odds. according to your pseudo-code, you should start moving the elements from the END, not the middle.
If this is a class assignment, shouldn't you do it the way it's asked for? This may be a minor point, but when I was teaching Math, if the students didn't do something the way it was supposed to be done, I woudn't give them full credit.
I don't want to start an educational debate, but this should be considered.
f
 
Ranch Hand
Posts: 8934
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why are you doing this?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's something to play around with. Compare the two, you might spot a difference.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!