1

I am new to java. I am creating a school project in which I am using arrays.
My question is:
what is better - if i sort an array it also takes time in sorting.So will it be good to leave it unsorted for my small school project and while retrieving i will put some logic to retrive the desired array value.

  • 1
    so what do u think putting the logic for retrival won't take time. – iAmLearning Oct 29 '13 at 12:20
  • 1
    Depends. You can do sorted insertion if you need the array to be sorted at all stages of your algortithm. If you need it sorted only at a specific point, you're probably better off sorting it once it's been filled with items. – Niklas R Oct 29 '13 at 12:26
  • You should show what you have tried and understand – phuclv Oct 29 '13 at 12:37
  • [Click here][1] to get your answer. [1]: http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array?rq=1 – hungrycoder Oct 29 '13 at 13:02
  • Hope you may find the best answer [here][1]. [1]: http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array?rq=1 – hungrycoder Oct 29 '13 at 13:03

3 Answers3

3

It's better to keep it sorted if you think you will need sorted values at some later stage.
It would be better if you would paste your code here.

Eel Lee
  • 3,513
  • 2
  • 31
  • 49
iAmLearning
  • 1,153
  • 3
  • 15
  • 28
0

Either you sorted or not it is necessary to apply a logic to retrieve values from an array. If your array not a very large one you may not want to bother about performance since sorting doesn't take much time for smaller arrays.

It is better to use List over arrays since it has an implementation called ArrayList which grows dynamically.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

If it is possible, may be is better use a vector

Have you think what would happen when you insert a new item in your array? Or if you change the way of sorting? If you keep the array sorted, keep in mind this things.

For these reasons, I think is better to sort the array each time you need to return a value (the best is using Vectors, as I said at the beginning).

MTG
  • 551
  • 3
  • 14