0

I have a pair of arrays, one non sorted ( filled with Random ( numbers between 1-100 ) ) and one in descending order. I am using either of bubble sort, selection sort or insertion sort in order to sort them in ascending order. Contrary to my expectations, sorting the sorted array ( which is descending order sorted ) is faster (especially when the array size increases) for all algorithms. What is the reason for this?

Gas1991
  • 1
  • 2
  • 3
    Is the result [statistically significant](http://en.wikipedia.org/wiki/Statistical_significance)? Did the algorithms have proper warm up? – amit Nov 11 '13 at 09:45
  • How did you test that? – isnot2bad Nov 11 '13 at 09:46
  • refer this http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array?rq=1 – Prabhakaran Ramaswamy Nov 11 '13 at 09:47
  • with System.nanotime(); – Gas1991 Nov 11 '13 at 09:47
  • no it should be any difference, if you express algoritgh speed in big 'O' notation, both will be exact same, and if you try to find speed of algoright in time unit, dont forget order of your initial list might affect your final result, ergo it will be different for asc and desc sort – user902383 Nov 11 '13 at 09:47
  • 1
    @ByteCode In this case, both are sorted - one ascending the other descending, so it's not the same issue – amit Nov 11 '13 at 09:48
  • what is your sample size? – theDazzler Nov 11 '13 at 09:55
  • 1
    Well, your editted question fits perfectly now the link suggested by @ByteCode. – amit Nov 11 '13 at 10:07
  • Just to confirm, you are sorting the array (that's already sorted in **descending** order) in **ascending** order, right? (either way, it's likely (close enough to) a duplicate of [this](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array)) – Bernhard Barker Nov 11 '13 at 10:20

0 Answers0