Reverse the Array

Definition & Explanation

The reverse array operation in programming involves rearranging the elements of an array in the opposite order, essentially flipping it horizontally. This fundamental operation is crucial for various tasks like sorting and searching within data structures. Implementation usually requires iterating through the array and swapping elements symmetrically around the midpoint. Whether performed in place or on a new array, reverse array plays a significant role in algorithms for data manipulation. Its simplicity belies its importance in tasks involving array manipulation, making it an essential function in programming.

Program Logic

  1. Define a function called reverse_array that takes an array (or list) as input.
  2. Determine the length of the array using the len() function.
  3. Initialize two pointers, start pointing to the first element (index 0) and end pointing to the last element (index length - 1).
  4. Create a loop that runs as long as start is less than end.
  5. Inside the loop, swap the elements at positions start and end.
  6. Increment start and decrement end to move towards the center of the array.
  7. Once the loop completes, return the reversed array.
  8. This function will reverse the array in place, meaning it will modify the original array.
C

Method 1 :

#include <stdio.h>

void reverseArray(int arr[], int start, int end) {
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int length = sizeof(arr) / sizeof(arr[0]);

    reverseArray(arr, 0, length - 1);

    printf("Reversed Array: ");
    for (int i = 0; i < length; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
using namespace std;

void reverseArray(int arr[], int start, int end) {
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int length = sizeof(arr) / sizeof(arr[0]);

    reverseArray(arr, 0, length - 1);

    cout << "Reversed Array: ";
    for (int i = 0; i < length; i++) {
        cout << arr[i] << " ";
    }

    return 0;
}

Output :

JAVA

Method 1 :

public class Main {
    public static void reverseArray(int[] arr) {
        int start = 0;
        int end = arr.length - 1;

        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};

        reverseArray(arr);

        System.out.print("Reversed Array: ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

Output :

Python

Method 1 :

def reverselist(A,start,end):
    while start<end:
        A[start],A[end]=A[end],A[start]
        start+=1
        end-=1

A=[10,20,30,40,50]
reverselist(A,0,4)
print(A)

Output :

[50, 40, 30, 20, 10]