C Program to Reverse an Array

Here is the C program to reverse an array with and without using temporary array. Given an array of integers of length N, we have to reverse the sequence of array elements. The first element of array should become the last element and vice versa.

For Example

If input array is : 1 2 3 4 5 6 7
Reversed array should be : 7 6 5 4 3 2 1,

The algorithm to reverse an array is similar to the algorithm of reversing a string. Actually, a string is also an array of characters. We can reverse an array either by using a temporary array of same size or without using any extra memory by swapping elements.


C program to reverse elements of an array using extra memory

In below program, to reverse the elements of an array(inputArray) of length N we will use another array(outputArray) of same length. We will copy all the elements from inputArray to outputArray in reverse order such that last element of inputArray will become first element of outputArray and so on. Then we print the outputArray on screen using a for loop.

Time Complexity : O(n)
Space Complexity : O(n)

#include <stdio.h>

int main(){
    int inputArray[500], outputArray[500]; 
    int elementCount, counter;
    
    printf("Enter number of elements in array: ");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
    
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
    
    for(counter = 0; counter < elementCount; counter++){
        outputArray[counter] = inputArray[elementCount-counter-1];
    }    

    printf("Reversed Array\n");
    for(counter = 0; counter < elementCount; counter++){
        printf("%d ", outputArray[counter]);
    }
        
    return 0;
}
Output
Enter number of elements in array: 6
Enter 6 numbers
1 2 3 4 5 6
Reversed Array
6 5 4 3 2 1

C program to reverse an array by swapping elements

Algorithm to reverse an array by swapping elements
Let inputArray is an array of length N, and leftIndex and rightIndex are integer variables.
  • Initialize leftIndex and rightIndex with index of first and last element of inputArray respectively.
  • leftIndex = 0; and rightIndex = N - 1;.
  • Swap inputArray[leftIndex] and inputArray[rightIndex].
  • Now, Increment leftIndex (leftIndex++) and decrement rightIndex(rightIndex--).
  • Repeat last two steps, until leftIndex < rightIndex.

The advantage of this algorithm is that we don't need any temporary array. This algorithm is preferred, If we are working in an environment having strict memory constraints like embedded systems.

Time Complexity : O(n)
Space Complexity : O(1)
#include <stdio.h>

void swap(int *array, int leftIndex, int rightIndex);
int main(){
    int inputArray[500], elementCount, counter;
    int leftIndex, rightIndex;
    
    printf("Enter number of elements in array: ");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
    
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
    
    leftIndex = 0;
    rightIndex = elementCount-1;

    while(leftIndex < rightIndex){
        swap(inputArray, leftIndex, rightIndex);
        leftIndex++;
        rightIndex--;
    }
    
    printf("Reversed Array\n");
    for(counter = 0; counter < elementCount; counter++){
        printf("%d ", inputArray[counter]);
    }
        
    return 0;
}

void swap(int *array, int leftIndex, int rightIndex){
   int temp;
   temp = array[leftIndex];
   array[leftIndex] = array[rightIndex];
   array[rightIndex] = temp;
}
Output
Enter number of elements in array: 6
Enter 6 numbers 
6 -2 0 8 12 1
Reversed Array
1 12 8 0 -2 6

We can also use recursion to reverse an array, by reducing original problem to smaller sub-problem. Let reverse(inputArray, i, j) function reverses the elements of inputArray from index i to j. Then to reverse an array of length N, we can first swap the leftmost and rightmost element of array and then recursively reverse inner sub-array from index 1 to N-2.

reverse(inputArray, i, j) = swap(inputArray[i], inputArray[j]) + reverse(inputArray, i+1, j-1)

Click here to check c program to reverse an array using recursion.


Related Topics
C program to reverse a string
C program to sort characters of a string
C program to find sum of array elements using recursion
C program to delete duplicate elements from an array
C Program to find minimum element in an array
C program to find sum of digits of a number using recursion
C program for palindrome check using recursion
List of all C Programs