Table of contents

Reverse an array

Given an array, the task is to reverse the given array.

Examples:

Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Input: [10, 20, 30, 40]
Output: [40, 30, 20, 10]

How to reverse an array ?

  1. Start with two pointers: Initialize two pointers, one at the beginning (start) and one at the end (end) of the array.
  2. Swap the elements: Swap the element at the start pointer with the element at the end pointer.
  3. Move the pointers: Increment the start pointer and decrement the end pointer.
  4. Repeat the process until start is greater than or equal to end.

Example of Reversing an Array

Let’s take an example of reversing the array [10, 20, 30, 40, 50]:

  1. Initial List: [10, 20, 30, 40, 50]
  2. Step 1: Swap the first and last elements: [50, 20, 30, 40, 10]
  3. Step 2: Swap the second and second-last elements: [50, 40, 30, 20, 10]
  4. Step 3: The middle element remains in place, so the final reversed array is [50, 40, 30, 20, 10].

C++ Program to reverse an array

cpp
#include <iostream>
using namespace std;

// Function to reverse an array
void reverseArray(int arr[], int n) {
    int start = 0;  // Initialize the start pointer
    int end = n - 1;  // Initialize the end pointer

    // Swap elements from start to end until they meet in the middle
    while (start < end) {
        swap(arr[start], arr[end]);  // Swap the elements
        start++;  // Move start pointer to the right
        end--;  // Move end pointer to the left
    }
}

// Function to print the array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

// Main function
int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    cout << "Original array: ";
    printArray(arr, n);

    reverseArray(arr, n);

    cout << "Reversed array: ";
    printArray(arr, n);

    return 0;
}

Java Program to reverse an array

java
public class Main {
    // Function to reverse an array
    public static void reverseArray(int arr[]) {
        int start = 0;  // Initialize the start pointer
        int end = arr.length - 1;  // Initialize the end pointer

        // Swap elements from start to end until they meet in the middle
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;

            start++;  // Move start pointer to the right
            end--;  // Move end pointer to the left
        }
    }

    // Function to print the array
    public static void printArray(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // Main function
    public static void main(String[] args) {
        int arr[] = {10, 20, 30, 40, 50};

        System.out.println("Original array:");
        printArray(arr);

        reverseArray(arr);

        System.out.println("Reversed array:");
        printArray(arr);
    }
}

Python Program to reverse an array

python
# Function to reverse an array
def reverse_array(arr):
    start = 0  # Initialize the start pointer
    end = len(arr) - 1  # Initialize the end pointer

    # Swap elements from start to end until they meet in the middle
    while start < end:
        arr[start], arr[end] = arr[end], arr[start]  # Swap the elements
        start += 1  # Move start pointer to the right
        end -= 1  # Move end pointer to the left

# Function to print the array
def print_array(arr):
    print(" ".join(map(str, arr)))

# Main function
if __name__ == "__main__":
    arr = [10, 20, 30, 40, 50]

    print("Original array:")
    print_array(arr)

    reverse_array(arr)

    print("Reversed array:")
    print_array(arr)

DSA

Related Articles