Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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.

added 30 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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 .

mathjax bling
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

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

Source Link
Linus
  • 183
  • 2
  • 6
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /