C Program to Reverse an Array
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 , then the reversed array will look like .
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
Let us assume we are given an array of length , the steps of the algorithm to reverse it will be:
- Firstly, input the size and the elements of the array .
- Initialize an auxiliary array of size same as the array .
- Run a for loop with an integer , where and increments by in each iteration.
- Inside the for loop, assign to be .
- Then copy the elements of the array into the array .
- Print the array .
Algorithm for Reversing an Array by In-place Swapping
Let us assume we are given an array of length , the steps of the algorithm to reverse it will be:
- Firstly, input the size and the elements of the array .
- Assign a pointer at the first element of the array and a pointer at the last element of the array.
- Run a while loop while .
- Inside the while loop, initialize a variable to store the value of . Then assign to be and to be . Lastly, increment the pointer by and decrement the pointer by .
- The array 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:
While printing the reverse of the array , the whole of the array is traversed from the last element to the first element, i.e., iterations. Therefore, the time complexity of this method is .
Space Complexity:
No auxiliary space is used. Therefore its space complexity is .
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:
While printing the reverse of the array , the whole of the array is traversed from the last element to the first element, i.e., iterations. Therefore, the time complexity of this method is .
Space Complexity:
No auxiliary space is used. Therefore its space complexity is .
3. Reverse Array in C Using Iterative Method
In this method, we are using another array to store the reverse of the given array , we do the same by iterating over the given array in a for loop and assigning the value of to the first position of and so on.
Code
Output
Explanation
In the above implementation, we are reversing the array by assigning the element from reverse traversing the array to the adjacent array element by traversing it. At last, we are printing the array.
Complexity Analysis
Time Complexity:
The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is .
Space Complexity:
Another array of length is used to store the reverse of the array . Therefore its space complexity is .
4. Reverse Array in C Using an Auxiliary Array
In this method, we will be using an auxiliary array to temporarily store the reverse of the given array , and later we will modify the given array by copying 's elements to the array .
Code
Output
Explanation
In the above implementation, we are using an auxiliary array and assigning its first element to be 's last element, and so on.
Complexity Analysis
Time Complexity:
The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is .
Space Complexity:
An auxiliary array of length is used to store the reverse of the array . Therefore its space complexity is .
5. Reverse Array in C Using Function
In this method, we will use the same technique we used in the approach, i.e., the auxiliary array. We will pass the array and its size to the function which will reverse the given array .
Code
Output
Explanation
In the above implementation, we are reversing the array by calling the function which uses the auxiliary array technique to reverse the array passed to it.
Complexity Analysis
Time Complexity:
The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is .
Space Complexity:
Another array of length is used to store the reverse of the array . Therefore its space complexity is .
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 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 , we are creating another array and filling it backward with the elements of using a for a loop. As result, we get the reverse of the array as the array .
Complexity Analysis
Time Complexity:
The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is .
Space Complexity:
Another array of length is used to store the reverse of the array . Therefore its space complexity is .
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 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 , we are creating another array and filling it backward with the elements of using a while loop. As result, we get the reverse of the array as the array .
Complexity Analysis
Time Complexity:
The whole of the array is traversed while reversing it. Therefore, the time complexity of this method is .
Space Complexity:
Another array of length is used to store the reverse of the array . Therefore its space complexity is .
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 of size , let us assign two pointers at the first element ( position) and at the last element ( position). The steps to reverse the array will be:
- Swap the values at position ( index) and position ( index). The resultant array will be . Increment by and decrement by .
- Swap the values at position ( index) and position ( index). The resultant array will be . Increment by and decrement by .
- Swap the values at position ( index) and position ( index). The resultant array will be . Increment by and decrement by .
- Swap the values at position ( index) and position ( index). The resultant array will be . Increment by and decrement by .
- The pointer is at the index and the pointer is at the index. We can only continue if , that is not the case here so we will stop. Therefore the reversed array is .
Let's see how to implement this method.
Code
Output
Explanation
In the above implementation, we are reversing the given array using the in-place swapping technique by reading the elements from both ends of the array and swapping them.
Complexity Analysis
Time Complexity:
The pointer starts at position and it increments by in each iteration and the pointer starts at . The loop runs till and pointer meet, so there are total iterations. Therefore, the time complexity will be .
Space Complexity:
No auxiliary space is used in this method. Therefore its space complexity is .
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 condition which runs if , swaps elements at and position and then calls the function.
Code
Output
Explanation
In the above implementation, we are reversing the given array using the in-place swapping technique by reading the elements from both ends of the array and swapping them.
Complexity Analysis
Time Complexity:
The reverse function is a recursive function that runs till is less than , which would take recursive calls. Therefore, the time complexity is .
Space Complexity:
A stack data structure is used for memory allocation when a recursive function is called. Therefore, its space complexity is .
Conclusion
- 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.
- Reversing an array is a beginner's problem and an important one while learning arrays.
- We can reverse an array using an auxiliary array that takes space.
- 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.