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

  1. Start with Two Pointers: Use two variables (start and end) to point to the first and last elements of the array.
  2. Swap Elements: Swap the elements at the start and end positions.
  3. Move the Pointers: After the swap, move the start pointer forward and the end pointer backward.
  4. Repeat Until Middle: Keep swapping the elements until the start pointer crosses the end 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 and end are swapped.
  • Pointer Movement: After swapping, the start pointer moves forward, and the end pointer moves backward.
See also  How do you find the middle element of a linked list?

Complexity

  • Time Complexity: The time complexity is O(n), where n 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 variables start and end.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *