C Program to Reverse an Array

Topics Covered
Get FREE counselling from experts
Learn more about SCALER Premium Programs
Know more

Overview

In C, there are no inbuilt functions to reverse an array, we have to write the code for it on our own. An array can be reversed using different methods and algorithms - printing it from the last element, using an auxiliary array, recursion, swapping, etc.

For example, if the given array is [1,4,6,7,8,4,2,6,2][1, 4, 6, 7, 8, 4, 2, 6, 2], then the reversed array will look like [2,6,2,4,8,7,4,6,1][2, 6, 2, 4, 8, 7, 4, 6, 1].

Algorithm to Reverse an Array in C

Reversing an array is one of the easiest problems while learning the arrays topic, while there are several methods to reverse an array in C, all of them are very easy and interesting, two of them being the most efficient solutions; Using an auxiliary array and In-place Swapping.

Let's discuss their algorithms:

Algorithm for reversing an array using an Auxiliary array

reversing-array-using-auxiliary-array

Let us assume we are given an array arrarr of length nn, the steps of the algorithm to reverse it will be:

  1. Firstly, input the size nn and the elements of the array arrarr.
  2. Initialize an auxiliary array rarrrarr of size same as the array arrarr.
  3. Run a for loop with an integer i=0i = 0, where i<0i \lt 0 and ii increments by 11 in each iteration.
  4. Inside the for loop, assign rarr[i]rarr[i] to be arr[ni1]arr[n - i - 1].
  5. Then copy the elements of the array rarrrarr into the array arrarr.
  6. Print the array arrarr.

Algorithm for Reversing an Array by In-place Swapping

reversing-array-by-in-place-swapping

Let us assume we are given an array arrarr of length nn, the steps of the algorithm to reverse it will be:

  1. Firstly, input the size nn and the elements of the array arrarr.
  2. Assign a pointer l=0l = 0 at the first element of the array and a pointer r=n1r = n - 1 at the last element of the array.
  3. Run a while loop while l<rl \lt r.
  4. Inside the while loop, initialize a temptemp variable to store the value of arr[l]arr[l]. Then assign arr[l]arr[l] to be arr[r]arr[r] and arr[r]arr[r] to be temptemp. Lastly, increment the pointer ll by 11 and decrement the pointer rr by 11.
  5. The array arrarr is reversed now, just print it using a for a loop.

1. Reverse Array in C By Printing It from the Last Element

This method is useful when we just want to print the reverse of the given array without modifying the original array or using another array to store the reverse of the original array.

Code

Output

Explanation

In the above implementation, we are printing the reverse of the given array by printing it from the last element to the very first element.

Complexity Analysis

Time Complexity: O(n)O (n)

While printing the reverse of the array arrarr, the whole of the array is traversed from the last element to the first element, i.e., nn iterations. Therefore, the time complexity of this method is O(n)O(n).

Space Complexity: O(1)O (1)

No auxiliary space is used. Therefore its space complexity is O(1)O(1).

2. Reverse Array in C Using Pointers

In this method, we are initializing an array using a pointer and then printing its reverse, this approach is very similar to the first one.

Code

Output

Explanation

In the above implementation, we are initializing an array using a pointer and then print its reverse by printing it from the last element to the very first element.

Complexity Analysis

Time Complexity: O(n)O(n)

While printing the reverse of the array arrarr, the whole of the array is traversed from the last element to the first element, i.e., nn iterations. Therefore, the time complexity of this method is O(n)O(n).

Space Complexity: O(1)O(1)

No auxiliary space is used. Therefore its space complexity is O(1)O(1).

3. Reverse Array in C Using Iterative Method

In this method, we are using another array rarrrarr to store the reverse of the given array arrarr, we do the same by iterating over the given array in a for loop and assigning the (n1)th(n - 1)th value of arrarr to the first position of rarrrarr and so on.

Code

Output

Explanation

In the above implementation, we are reversing the array by assigning the element from reverse traversing the arrarr array to the adjacent rarrrarr array element by traversing it. At last, we are printing the rarrrarr array.

Complexity Analysis

Time Complexity: O(n)O(n)

The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is O(n)O(n).

Space Complexity: O(n)O(n)

Another array rarrrarr of length nn is used to store the reverse of the array arrarr. Therefore its space complexity is O(n)O(n).

4. Reverse Array in C Using an Auxiliary Array

In this method, we will be using an auxiliary array rarrrarr to temporarily store the reverse of the given array arrarr, and later we will modify the given array by copying rarrrarr's elements to the array arrarr.

Code

Output

Explanation

In the above implementation, we are using an auxiliary array rarrrarr and assigning its first element to be arrarr's last element, and so on.

Complexity Analysis

Time Complexity: O(n)O(n)

The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is O(n)O(n).

Space Complexity: O(n)O(n)

An auxiliary array rarrrarr of length nn is used to store the reverse of the array arrarr. Therefore its space complexity is O(n)O(n).

5. Reverse Array in C Using Function

In this method, we will use the same technique we used in the 4th4th approach, i.e., the auxiliary array. We will pass the array and its size to the function reverse()reverse() which will reverse the given array arrarr.

Code

Output

Explanation

In the above implementation, we are reversing the array by calling the reverse()reverse() function which uses the auxiliary array technique to reverse the array passed to it.

Complexity Analysis

Time Complexity: O(n)O(n)

The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is O(n)O(n).

Space Complexity: O(n)O(n)

Another array rarrrarr of length nn is used to store the reverse of the array arrarr. Therefore its space complexity is O(n)O(n).

6. Reverse Array in C Using For Loop

In this method, we will use a for loop to fill the auxiliary array with elements from the input array arrarr backward, and then copy the auxiliary array’s contents into the original one.

Code

Output

Explanation

In the above implementation, instead of reversing the original array arrarr, we are creating another array rarrrarr and filling it backward with the elements of arrarr using a for a loop. As result, we get the reverse of the array arrarr as the array rarrrarr.

Complexity Analysis

Time Complexity: O(n)O(n)

The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is O(n)O(n).

Space Complexity: O(n)O(n)

Another array rarrrarr of length nn is used to store the reverse of the array arrarr. Therefore its space complexity is O(n)O(n).

7. Reverse Array in C Using While Loop

In this method, we will use a while loop to fill the auxiliary array with elements from the input array arrarr backward, and then copy the auxiliary array’s contents into the original one.

Code

Output

Explanation

In the above implementation, instead of reversing the original array arrarr, we are creating another array rarrrarr and filling it backward with the elements of arrarr using a while loop. As result, we get the reverse of the array arrarr as the array rarrrarr.

Complexity Analysis

Time Complexity: O(n)O(n)

The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is O(n)O(n).

Space Complexity: O(n)O(n)

Another array rarrrarr of length nn is used to store the reverse of the array arrarr. Therefore its space complexity is O(n)O(n).

8. Reverse Array in C by Swapping

In this method, instead of using an auxiliary array, we will use the in-place swapping technique.

Let's understand this method by an example, We are given an array arr=[1,5,8,5,3,55,22,2]arr = [1, 5, 8, 5, 3, 55, 22, 2] of size n=8n = 8, let us assign two pointers ll at the first element (0th0 _ { th } position) and rr at the last element (7th7 _ { th } position). The steps to reverse the array will be:

  1. Swap the values at lthl _ {th} position (00 index) and rthr _ {th} position (77 index). The resultant array will be [2,5,8,5,3,55,22,1][\mathbf {2}, 5, 8, 5, 3, 55, 22, \mathbf {1}]. Increment ll by 11 and decrement rr by 11.
  2. Swap the values at lthl _ {th} position (11 index) and rthr _ {th} position (66 index). The resultant array will be [2,22,8,5,3,55,5,1][\mathbf {2}, \mathbf {22}, 8, 5, 3, 55, \mathbf {5}, \mathbf {1}]. Increment ll by 11 and decrement rr by 11.
  3. Swap the values at lthl _ {th} position (22 index) and rthr _ {th} position (55 index). The resultant array will be [2,22,55,5,3,8,5,1][\mathbf {2}, \mathbf {22}, \mathbf {55}, 5, 3, \mathbf {8}, \mathbf {5}, \mathbf {1}]. Increment ll by 11 and decrement rr by 11.
  4. Swap the values at lthl _ {th} position (33 index) and rthr _ {th} position (44 index). The resultant array will be [2,22,55,3,5,8,5,1][\mathbf {2}, \mathbf {22}, \mathbf {55}, \mathbf {3}, \mathbf{5}, \mathbf {8}, \mathbf {5}, \mathbf {1}]. Increment ll by 11 and decrement rr by 11.
  5. The ll pointer is at the 4th4th index and the rr pointer is at the 3rd3rd index. We can only continue if l<rl \lt r, that is not the case here so we will stop. Therefore the reversed array is [2,22,55,3,5,8,5,1][\mathbf {2}, \mathbf {22}, \mathbf {55}, \mathbf {3}, \mathbf{5}, \mathbf {8}, \mathbf {5}, \mathbf {1}].

Let's see how to implement this method.

Code

Output

Explanation

In the above implementation, we are reversing the given array arrarr using the in-place swapping technique by reading the elements from both ends of the array and swapping them.

Complexity Analysis

Time Complexity: O(n)O(n)

The pointer ll starts at 0th0th position and it increments by 11 in each iteration and the pointer rr starts at (n1)th(n - 1)th. The loop runs till ll and rr pointer meet, so there are total n/2n / 2 iterations. Therefore, the time complexity will be O(n)O(n).

Space Complexity: O(1)O(1)

No auxiliary space is used in this method. Therefore its space complexity is O(1)O(1).

9. Reverse Array in C Using Recursion

In this method, we will use recursion to implement the in-place swapping technique that we discussed in the previous approach. Instead of a for loop, we are just using an ifif condition which runs if l<rl \lt r, swaps elements at lthl _ { th } and rthr _ { th } position and then calls the reverse(arr,l+1,r1)reverse ( arr, l + 1, r - 1 ) function.

Code

Output

Explanation

In the above implementation, we are reversing the given array arrarr using the in-place swapping technique by reading the elements from both ends of the array and swapping them.

Complexity Analysis

Time Complexity: O(n)O(n)

The reverse function is a recursive function that runs till ll is less than rr, which would take n/2n / 2 recursive calls. Therefore, the time complexity is O(n)O(n).

Space Complexity: O(n)O(n)

A stack data structure is used for memory allocation when a recursive function is called. Therefore, its space complexity is O(n)O(n).

Conclusion

  1. There are no inbuilt functions in the C programming language to reverse an array, we will have to implement the methods on our own to do the same.
  2. Reversing an array is a beginner's problem and an important one while learning arrays.
  3. We can reverse an array using an auxiliary array that takes nn space.
  4. To avoid taking extra space, we can reverse the array using the in-place swapping technique, which is the most efficient algorithm for reversing an array.