Accessing an element in Java is \$O(1)\$. Both methods do \$O(n)\$ comparisons (n = number of elements). The fist one has \$O(2n)\$ accesses, the second one only \$O(n)\$, but you store a extra variable.
I would prefer the first one, cause storing costs often more time.
By the way, why don't you just sort them, using int[]?
Sorting an already sorted array often costs (depending on the implementation) also \$O(n)\$. The implementation of the standard Java array-sort is documented here.
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element accessing element.
Accessing an element in Java is \$O(1)\$. Both methods do \$O(n)\$ comparisons (n = number of elements). The fist one has \$O(2n)\$ accesses, the second one only \$O(n)\$, but you store a extra variable.
I would prefer the first one, cause storing costs often more time.
By the way, why don't you just sort them, using int[]?
Sorting an already sorted array often costs (depending on the implementation) also \$O(n)\$. The implementation of the standard Java array-sort is documented here.
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element.
Accessing an element in Java is \$O(1)\$. Both methods do \$O(n)\$ comparisons (n = number of elements). The fist one has \$O(2n)\$ accesses, the second one only \$O(n)\$, but you store a extra variable.
I would prefer the first one, cause storing costs often more time.
By the way, why don't you just sort them, using int[]?
Sorting an already sorted array often costs (depending on the implementation) also \$O(n)\$. The implementation of the standard Java array-sort is documented here.
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element.
Accessing an element in Java is \$O(1)\$. Both methods do \$O(n)\$ comparisons (n = number of elements). The fist one has \$O(2n)\$ accesses, the second one only \$O(n)\$, but you store a extra variable.
I would prefer the first one, cause storing costs often more time.
By the way., why don't you just sort them, using int[]? Using: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])
Sorting an already sorted array often costs (depending on the implementation) also \$O(n)\$. The implementation of the standard Java array-sort is documented here: http://www.docjar.com/html/api/java/util/Arrays.java.htmlhere .
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element: http://stackoverflow.com/questions/20615908/array-access-complexityaccessing element .
Accessing an element in Java is \$O(1)\$. Both methods do \$O(n)\$ comparisons (n = number of elements). The fist one has \$O(2n)\$ accesses, the second one only \$O(n)\$, but you store a extra variable.
I would prefer the first one, cause storing costs often more time.
By the way. why don't you just sort them? Using: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])
Sorting an already sorted array often costs (depending on the implementation) also \$O(n)\$. The implementation of the standard Java array-sort is documented here: http://www.docjar.com/html/api/java/util/Arrays.java.html
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element: http://stackoverflow.com/questions/20615908/array-access-complexity
Accessing an element in Java is \$O(1)\$. Both methods do \$O(n)\$ comparisons (n = number of elements). The fist one has \$O(2n)\$ accesses, the second one only \$O(n)\$, but you store a extra variable.
I would prefer the first one, cause storing costs often more time.
By the way, why don't you just sort them, using int[]?
Sorting an already sorted array often costs (depending on the implementation) also \$O(n)\$. The implementation of the standard Java array-sort is documented here .
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element .
Accessing an element in Java is O(1)\$O(1)\$. Both methods do O(n)\$O(n)\$ comparisons (n = number of elements). The fist one has O(2n)\$O(2n)\$ accesses, the firstsecond one only O(n)\$O(n)\$, but you store a extra variable. I
I would prefer the first one, cause storing costs often more time.
By the way. why don't you just sort them? Using: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])
Sorting an already sorted array often costs (depending on the implementation) also O(n)\$O(n)\$. The implementation of the standard Java array-sort is documented here: http://www.docjar.com/html/api/java/util/Arrays.java.html
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element: http://stackoverflow.com/questions/20615908/array-access-complexity
Accessing an element in Java is O(1). Both methods do O(n) comparisons (n = number of elements). The fist one has O(2n) accesses, the first one only O(n), but you store a extra variable. I would prefer the first one, cause storing costs often more time.
By the way. why don't you just sort them? Using: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])
Sorting an already sorted array often costs (depending on the implementation) also O(n). The implementation of the standard Java array-sort is documented here: http://www.docjar.com/html/api/java/util/Arrays.java.html
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element: http://stackoverflow.com/questions/20615908/array-access-complexity
Accessing an element in Java is \$O(1)\$. Both methods do \$O(n)\$ comparisons (n = number of elements). The fist one has \$O(2n)\$ accesses, the second one only \$O(n)\$, but you store a extra variable.
I would prefer the first one, cause storing costs often more time.
By the way. why don't you just sort them? Using: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])
Sorting an already sorted array often costs (depending on the implementation) also \$O(n)\$. The implementation of the standard Java array-sort is documented here: http://www.docjar.com/html/api/java/util/Arrays.java.html
I think if you want to sort the array if it is not sorted just use this sorting method.
References: accessing element: http://stackoverflow.com/questions/20615908/array-access-complexity