C Program to Reverse an Array

  • Write a C program to reverse the sequence of array elements.
  • C program to reverse an array 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)

/*
* C Program to reverse an array of Integers
* using extra memory
*/
#include <stdio.h>
#include <conio.h>

int main(){
    int inputArray[500], outputArray[500], 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]);
    }
    
    /* Copy numbers from inputArray to outputArray in 
       reverse order */
    for(counter = 0; counter < elementCount; counter++){
        outputArray[counter] = inputArray[elementCount-counter-1];
    }    
    /* Print Reversed array */
    printf("Reversed Array\n");
    for(counter = 0; counter < elementCount; counter++){
        printf("%d ", outputArray[counter]);
    }
        
    getch();
    return 0;
}

Program 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)
/*
* C Program to reverse an array by swapping elements
*/
#include <stdio.h>
#include <conio.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;
    /* Swapping left and Right elements*/
    while(leftIndex < rightIndex){
        swap(inputArray, leftIndex, rightIndex);
        leftIndex++;
        rightIndex--;
    }
    printf("Reversed Array\n");
    /* Print Reversed array */
    for(counter = 0; counter < elementCount; counter++){
        printf("%d ", inputArray[counter]);
    }
        
    getch();
    return 0;
}
/*
*  Function to swap two numbers in array
*/
void swap(int *array, int leftIndex, int rightIndex){
   int temp;
   temp = array[leftIndex];
   array[leftIndex] = array[rightIndex];
   array[rightIndex] = temp;
}

Program 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