I am trying to reverse the order of an Array in java.
What is the most efficient way to do so in O(n) with the least amount of memory used.
No need to answer with co
pseudocode, assuming 0-based-index arrays:
for i in range(0, len(array)/2):
swap(array[i], array[(len(array)-1)-i])
Here's two solutions:
loop to N/2
swap each element at i with element at N - i
Another solution is (depending on your circumstances) fake reversing the array by indexing:
GetValueAt(int i){return array[N - i];}