Reversing an Array in C: A Step-by-Step Guide
Introduction
Arrays are a fundamental data structure in C programming, used to store collections of elements of the same data type. Reversing an array is a common operation that can be performed to transform the array into its reverse order. In this article, we will explore the different methods to reverse an array in C, including manual reversal and using built-in functions.
Manual Reversal
Manual reversal involves iterating through the array and swapping each element with its next neighbor. This method is simple but inefficient for large arrays.
Code:
#include <stdio.h>
// Function to reverse an array
void reverseArray(int arr[], int size) {
int temp, i, j;
for (i = 0; i < size / 2; i++) {
// Swap arr[i] with arr[size - i - 1]
temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
}
// Function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("n");
}
// Example usage
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, size);
reverseArray(arr, size);
printf("Reversed array: ");
printArray(arr, size);
return 0;
}
Output:
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
Time Complexity: O(n/2) = O(n), where n is the size of the array.
Space Complexity: O(1), as no additional space is required.
Built-in Functions
C provides two built-in functions to reverse an array: reverse() and reverseArray().
Code:
#include <stdio.h>
#include <stdlib.h>
// Function to reverse an array using reverse()
void reverse(int arr[], int size) {
int* temp = (int*)malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
temp[i] = arr[i];
}
for (int i = 0; i < size / 2; i++) {
// Swap arr[i] with arr[size - i - 1]
temp[i] = arr[size - i - 1];
arr[size - i - 1] = temp[i];
}
free(temp);
}
// Function to reverse an array using reverseArray()
void reverseArray(int arr[], int size) {
reverseArrayHelper(arr, size);
}
// Helper function to reverse an array
void reverseArrayHelper(int arr[], int size) {
int* temp = (int*)malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
temp[i] = arr[i];
}
for (int i = 0; i < size / 2; i++) {
// Swap arr[i] with arr[size - i - 1]
temp[i] = arr[size - i - 1];
arr[size - i - 1] = temp[i];
}
free(temp);
}
// Function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("n");
}
// Example usage
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, size);
reverseArray(arr, size);
printf("Reversed array: ");
printArray(arr, size);
return 0;
}
Output:
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
Time Complexity: O(n), where n is the size of the array.
Space Complexity: O(n), as the array is resized to store the reversed elements.
Conclusion
Reversing an array in C can be achieved using manual methods or built-in functions. The manual method is simple but inefficient for large arrays, while the built-in functions reverse() and reverseArray() provide a more efficient solution. By understanding the time and space complexity of these methods, developers can choose the most suitable approach for their specific use case.
