0

Just a quick question.

Which option would be more efficient?

  1. Sorting a 2D array (Each value in the 1st dimension of the array is linked to the value in the 2nd so they must be sorted equally [value and ID number] ).

or

  1. Sort a 1D array and then compare(using linear search) values against another set of values to check if they match (in order to find which ID number corresponds to each value).

All values are guaranteed to be different so there is no problem with duplication of numbers. Even if there was it wouldn't matter.

Is there a way of finding out how efficient both methods are in the debugger?

Thank you all for your time. =]

Seb

Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
  • How many times do you need to look up the value? If you only need to lookup once, then skip the sorting. If you need to lookup many times then sort both dimensions and search with a binary search. – Roger Lindsjö Sep 04 '12 at 11:22

1 Answers1

0

I think using the 2D array sorting technique with the help of using a comparator will be much more efficient. In this technique , the array is sorted in one go by just one unit of comparison for each 2D array and in the 2nd approach you are first sorting the 1D array and then again searching to map the associated values.

example : sorting 2D array of String in java

You can try out the time required for sorting yourself by printing the timestamp before sorting and after sorting for both above approaches.

Community
  • 1
  • 1
Metalhead
  • 1,429
  • 3
  • 15
  • 34