Reversing an array means rearranging its elements such that the first element becomes the last, the second element becomes the second last, and so on. In simple terms, it means flipping the array around. For example, if you have an array [1, 2, 3, 4, 5]
, reversing it will result in [5, 4, 3, 2, 1]
.
Steps to Reverse an Array
- Start with Two Pointers: Use two variables (
start
andend
) to point to the first and last elements of the array. - Swap Elements: Swap the elements at the
start
andend
positions. - Move the Pointers: After the swap, move the
start
pointer forward and theend
pointer backward. - Repeat Until Middle: Keep swapping the elements until the
start
pointer crosses theend
pointer.
Example
If you have an array [1, 2, 3, 4, 5]
, the steps will look like this:
- Initial array:
[1, 2, 3, 4, 5]
- Swap the first (
1
) and last (5
) elements:[5, 2, 3, 4, 1]
- Move the pointers inward. Now, swap the second (
2
) and second last (4
) elements:[5, 4, 3, 2, 1]
- The middle element (
3
) stays in place, and the array is now reversed.
Code Implementation in Different Languages
1. Python
def reverse_array(arr):
start = 0
end = len(arr) - 1
while start < end:
# Swap elements at start and end
arr[start], arr[end] = arr[end], arr[start]
# Move the pointers
start += 1
end -= 1
return arr
# Example
arr = [1, 2, 3, 4, 5]
print(reverse_array(arr)) # Output: [5, 4, 3, 2, 1]
2. JavaScript
function reverseArray(arr) {
let start = 0;
let end = arr.length - 1;
while (start < end) {
// Swap elements at start and end
[arr[start], arr[end]] = [arr[end], arr[start]];
// Move the pointers
start++;
end--;
}
return arr;
}
// Example
let arr = [1, 2, 3, 4, 5];
console.log(reverseArray(arr)); // Output: [5, 4, 3, 2, 1]
3. C++
#include <iostream>
#include <vector>
using namespace std;
void reverseArray(vector<int>& arr) {
int start = 0;
int end = arr.size() - 1;
while (start < end) {
// Swap elements at start and end
swap(arr[start], arr[end]);
// Move the pointers
start++;
end--;
}
}
// Example
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
reverseArray(arr);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " "; // Output: 5 4 3 2 1
}
return 0;
}
4. Java
import java.util.Arrays;
public class ReverseArray {
public static void reverseArray(int[] arr) {
int start = 0;
int end = arr.length - 1;
while (start < end) {
// Swap elements at start and end
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Move the pointers
start++;
end--;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
reverseArray(arr);
System.out.println(Arrays.toString(arr)); // Output: [5, 4, 3, 2, 1]
}
}
Explanation of Code
- Start Pointer: This points to the beginning of the array.
- End Pointer: This points to the end of the array.
- While Loop: It runs until the start pointer is smaller than the end pointer. This ensures that the swap happens only until the middle of the array.
- Swapping: The values at the
start
andend
are swapped. - Pointer Movement: After swapping, the
start
pointer moves forward, and theend
pointer moves backward.
Complexity
- Time Complexity: The time complexity is
O(n)
, wheren
is the number of elements in the array because each element is processed once. - Space Complexity: The space complexity is
O(1)
because no additional space is used other than the variablesstart
andend
.