• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Maintain sorted order or Insertion Sort  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

I have a list of items in an ArrayList. The list is being added to one at a time and I want to maintain a sorted list. So my question is this: is it more efficient to insert the item in the correct position or to insert the item at the end and then use insertion sort which is extremely fast for very slightly unsorted arrays. I'd really appreciate any help!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that you want to sort your ArrayList, if so you don't have to worry about Insertion sort or the way you have import items to your array list, there is a sort function in Collections class for help. Please refer to this link to see the use in detail.
 
Marshal
Posts: 60142
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing to stop you implementing your own insertion sort algorithm, but it is usually better to use ready‑made methods if possible. Remember the sort() method has been tested by used millions of times.
 
author & internet detective
Sheriff
Posts: 38569
659
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have duplicates in your list? If not, you can use a TreeSet which will take care of sorting for you.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!