Reverse an Array: flowchart, algorithm and program

This program reads n numbers into an array, reverses the array in place, and prints it. It swaps the first element with the last, the second with the second-last, and so on until it reaches the middle, so no second array is needed.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: Set i = 0.
  4. Step 4: If i < n, go to step 5; otherwise go to step 7.
  5. Step 5: Read arr[i].
  6. Step 6: Increase i by 1. Go to step 4.
  7. Step 7: Set i = 0.
  8. Step 8: If i < n/2, go to step 9; otherwise go to step 13.
  9. Step 9: Set temp = arr[i].
  10. Step 10: Set arr[i] = arr[n-1-i].
  11. Step 11: arr[n-1-i] = temp.
  12. Step 12: Increase i by 1. Go to step 8.
  13. Step 13: Set i = 0.
  14. Step 14: If i < n, go to step 15; otherwise go to step 17.
  15. Step 15: Print arr[i].
  16. Step 16: Increase i by 1. Go to step 14.
  17. Step 17: Print newline.
  18. Step 18: Stop.

Pseudocode

BEGIN
    READ n
    i ← 0
    WHILE i < n DO
        READ arr[i]
        i++
    END WHILE
    i ← 0
    WHILE i < n/2 DO
        temp ← arr[i]
        arr[i] ← arr[n-1-i]
        arr[n-1-i] ← temp
        i++
    END WHILE
    i ← 0
    WHILE i < n DO
        PRINT arr[i]
        i++
    END WHILE
    PRINT newline
END

Program in C, Python and Java

Reverse an Array program in C

#include <stdio.h>

int main() {
    int n, i, temp;
    int arr[100];

    printf("Enter n: ");
    scanf("%d", &n);

    printf("Enter %d numbers: ", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    for (i = 0; i < n / 2; i++) {
        temp = arr[i];
        arr[i] = arr[n - 1 - i];
        arr[n - 1 - i] = temp;
    }

    printf("Reversed array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

Reverse an Array program in Python

n = int(input("Enter n: "))

print("Enter", n, "numbers: ", end="")
arr = list(map(int, input().split()))

for i in range(n // 2):
    temp = arr[i]
    arr[i] = arr[n - 1 - i]
    arr[n - 1 - i] = temp

print("Reversed array:", end=" ")
for i in range(n):
    print(arr[i], end=" ")
print()

Reverse an Array program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter n: ");
        int n = sc.nextInt();
        int[] arr = new int[n];

        System.out.print("Enter " + n + " numbers: ");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        for (int i = 0; i < n / 2; i++) {
            int temp = arr[i];
            arr[i] = arr[n - 1 - i];
            arr[n - 1 - i] = temp;
        }

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

Sample input: n = 5, arr = 1 2 3 4 5
Output: Enter n: Enter 5 numbers: Reversed array: 5 4 3 2 1

How it works

  • The first loop reads n numbers into arr[0] to arr[n-1] using scanf.
  • The second loop runs only while i < n / 2, so it goes through just the first half of the array.
  • In each pass, arr[i] is paired with its mirror position arr[n - 1 - i]. For n = 5, i = 0 pairs with index 4, and i = 1 pairs with index 3.
  • The swap uses temp: save arr[i] in temp, copy arr[n - 1 - i] into arr[i], then put temp into arr[n - 1 - i]. Without temp, one value would be overwritten and lost.
  • For an odd n, the middle element (index 2 when n = 5) has no partner and stays where it is, which is correct. The last loop prints the array from index 0 to n-1.

Dry run (trace table)

StepLineBoxniarrtempOutput
13Start
28Read n5Enter n: Enter 5 numbers:
311i = 050
411i < n?50
512Read arr[i]50[1]
611i++51[1]
711i < n?51[1]
812Read arr[i]51[1,2]
911i++52[1,2]
1011i < n?52[1,2]
1112Read arr[i]52[1,2,3]
1211i++53[1,2,3]
1311i < n?53[1,2,3]
1412Read arr[i]53[1,2,3,4]
1511i++54[1,2,3,4]
1611i < n?54[1,2,3,4]
1712Read arr[i]54[1,2,3,4,5]
1811i++55[1,2,3,4,5]
1911i < n?55[1,2,3,4,5]
2015i = 050[1,2,3,4,5]

Time and space complexity

Time: O(n) · Space: O(1). Each loop runs at most n times with constant work per step, and only a temp variable is used beyond the input array.

Common mistakes

  • Looping i all the way to n instead of n / 2. Every pair then gets swapped twice, and the array ends up in its original order.
  • Writing arr[n - i] instead of arr[n - 1 - i]. Arrays start at index 0, so the last element is at n - 1, and arr[n - i] reads outside the array when i = 0.
  • Swapping without a temp variable (arr[i] = arr[n-1-i]; arr[n-1-i] = arr[i];). The first assignment overwrites arr[i], so both positions end up with the same value.

Viva questions

Q1. Why does the swapping loop on line 15 run only up to n / 2 and not up to n?

Each pass swaps two elements, one from the front and one from the back. After n/2 passes every pair has been swapped. If the loop ran to n, it would swap them all back and the array would end up in its original order.

Q2. What is the purpose of the variable temp on lines 16 to 18?

temp holds arr[i] for a moment so it isn't lost when arr[i] is overwritten with arr[n-1-i]. Without it both positions would end up with the same value.

Q3. Why is the index n - 1 - i used for the partner element, and not n - i?

Array indices run from 0 to n-1, so the last element is at n-1. For i = 0 the partner is n-1, for i = 1 it is n-2, and so on. n - i would give arr[n], which is outside the array.

Q4. What happens when n is odd, for example n = 5? What about the middle element?

n/2 is 2 because of integer division, so only the pairs (0,4) and (1,3) are swapped. The middle element at index 2 stays where it is, which is correct because it is its own mirror.

Q5. How does the program behave for n = 1, n = 0, or a negative n?

For n = 1, n/2 is 0, so no swaps happen and the single element is printed unchanged. For n = 0 or a negative n, the loops don't run and nothing is printed except the messages. However, the program never checks n, so a negative n is not rejected.

Q6. What goes wrong if the user enters n greater than 100?

arr has only 100 elements, so reading more writes past the end of the array. That is a buffer overflow and gives undefined behaviour. The program should check that n is between 1 and 100 before reading.

Q7. What are the time and space complexity of this program?

Time is O(n): the loop reads n elements, makes n/2 swaps and prints n elements. Extra space is O(1), because the reversal is done in place using only one temp variable (the array itself is a fixed 100 ints).

Q8. What would you change to reverse the array using a while loop with two pointers, or to print it in reverse without modifying the array?

For the two-pointer version, set low = 0 and high = n-1 and swap while low < high, moving low up and high down each time. To only display it reversed, skip the swap loop and print with a loop from i = n-1 down to 0.

Frequently asked questions

Why does the loop run only n / 2 times?

Each swap fixes two elements at once, one from each end. After n / 2 swaps every element is in its final place, and going further would undo the work.

What happens to the middle element when n is odd?

It stays where it is. With n = 5, n / 2 is 2, so only the pairs (0, 4) and (1, 3) are swapped, and index 2 is its own mirror.

What is the time and space complexity of reversing an array in place?

Time is O(n), since about n / 2 swaps are done. Extra space is O(1), because only the single temp variable is used besides the array.

More programs

← All programs

Try AI Flowchart free →