Reverse Array in C
Reversing an array within the C programming language entails flipping its elements' positions so that the primary detail turns into the final, the second detail becomes the second one-to-closing, and so forth. You can accomplish this by looping through the array and switching the elements at opposite ends.
Now let us see few examples:
Example 1:
Input:[1,2,3,4,5]
Output:[5,4,3,2,1]
Example 2:
Input:[10,20,30,40,50,60,70,80,90]
Output:[90,80,70,60,50,40,30,20,10]
Approach 1: Using loops
ALGORITHM:
Step 1: Start the program
Step 2: Now initialize the start and end where start=0 & end=n-1, where n is the length of array
Step 3: The start element will now be swapped with the end element index until the start is larger than or equal to the end.
Step 4: for every iteration, increase start and decrease end.
Step 5: The reversed array will be obtained by swapping the elements.
Implementation:
The implementation of the above steps given below
#include<stdio.h> void reverseArray(int arr[],int length) { int start = 0; int end = length - 1; int temp; while (start<end) { // Swap the elements at the start and end indices temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // Move the start and end indices towards each other start++; end--; } } int main() { int arr1[]={1,2,3,4,5}; int length1=5; int arr2[]={10,20,30,40,50,60,70,80,90}; int length2=9; printf("Original arrays:\n"); printf("arr1: "); for(int i =0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for(int i =0;i<length2;i++) { printf("%d",arr2[i]); } printf("\n"); reverseArray(arr1, length1); reverseArray(arr2, length2); printf("Reversed arrays:\n"); printf("arr1: "); for(int i =0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for(int i=0;i<length2;i++) { printf("%d ",arr2[i]); } printf("\n"); return 0; }
Output:
Original arrays: arr1:1 2 3 4 5 arr2:10 20 30 40 50 60 70 80 90 Reversed arrays: arr1: 5 4 3 2 1 arr2: 90 80 70 60 50 40 30 20 10
Approach 2: Using Recursion
ALGORITHM:
Step 1: create the function to reverse the array recursively.
Step 2: Now start index, end index, and array are given as parameters to the function.
Step 3: If the starting index is more than or equal to the end index return.
Step 4: Swap the elements of the start and end index.
Step 5: Call the function recursively using incrementing the start index through 1 and decrementing the end index with 1.
Step 6: In the main function call the recursive function with the start index set as zero and the end index set as the length of the array minus 1.
Step 7: Print the reversed array.
Implementation:
The implementation of the above steps given below
#include<stdio.h> void reverseArrayRecursive(int arr[],int start,int end) { if (start >= end) { // Base case: array has been reversed return; } // Swap the elements at the start and end indices int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // Recursively reverse the remaining subarray reverseArrayRecursive(arr,start + 1,end - 1); } int main() { int arr1[] = {1, 2, 3, 4, 5}; int length1 = 5; int arr2[] = {10, 20, 30, 40, 50, 60, 70, 80, 90}; int length2 = 9; printf("Original arrays:\n"); printf("arr1: "); for(int i =0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for(int i =0;i<length2;i++) { printf("%d",arr2[i]); } printf("\n"); reverseArrayRecursive(arr1,0,length1 - 1); reverseArrayRecursive(arr2,0,length2 - 1); printf("Reversed arrays:\n"); printf("arr1: "); for(int i =0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for(int i =0;i<length2;i++) { printf("%d",arr2[i]); } printf("\n"); return 0; }
Output:
Original arrays: arr1:1 2 3 4 5 arr2:10 20 30 40 50 60 70 80 90 Reversed arrays: arr1: 5 4 3 2 1 arr2: 90 80 70 60 50 40 30 20 10
Approach 3: Using Pointers
ALGORITHM:
Step 1: Create two pointers, start and end, that point to the first and last elements of the array respectively.
Step 2: While start is less than end, swap the values pointed to by start and end.
Step 3: Move start and end pointers to the next elements.
Step 4: Repeat steps 2-3 until start is greater than or equal to the end indicating that the entire array has been reversed.
Implementation:
The implementation of the above steps given below
#include<stdio.h> void reverseArrayPointer(int *arr,int length) { int *start = arr; // Pointer to the start of the array int *end = arr + length - 1; // Pointer to the end of the array while (start<end) { // Swap the elements pointed to by start and end int temp = *start; *start = *end; *end = temp; // Move start and end pointers to the next elements start++; end--; } } int main(){ int arr1[]={1, 2, 3, 4, 5}; int length1 = 5; int arr2[]={10, 20, 30, 40, 50, 60, 70, 80, 90}; int length2 = 9; printf("Original arrays:\n"); printf("arr1: "); for(int i =0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for(int i =0;i<length2;i++) { printf("%d",arr2[i]); } printf("\n"); reverseArrayPointer(arr1,length1); reverseArrayPointer(arr2,length2); printf("Reversed arrays:\n"); printf("arr1: "); for(int i =0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for(int i =0;i<length2;i++) { printf("%d",arr2[i]); } printf("\n"); return 0; }
Output:
Original arrays: arr1:1 2 3 4 5 arr2:10 20 30 40 50 60 70 80 90 Reversed arrays: arr1: 5 4 3 2 1 arr2: 90 80 70 60 50 40 30 20 10
Approach 4: Using Stack
ALGORITHM:
Step 1: Define a stack structure with an integer array and a top index variable.
Step 2: Set the top index value to -1 to define the stack.
Step 3: Make a function that examine to see whether the top index is 0 to see if the stack is empty.
Step 4: Specify a function that increments the top index and units the value of the element at that position to the specified value after figuring out whether the stack is full.
Step 5: Define a function to pop an element from the stack through first checking if the stack is empty and if no longer return the price of the element at the pinnacle index and decrementing the pinnacle/top index.
Step 6: Define a function to reverse array the usage of a stack by way of developing a stack and initializing it then pushing all elements of the array onto the stack then popping all elements from the stack and placing them again into the array.
Step 7: In the main function define two integer arrays with their respective lengths and print out the original array.
Step 8: Call the reverseArrayStack function on both arrays to reverse their order.
Step 9: Print out the reversed arrays.
Implementation:
The implementation of the above steps given below
#include<stdio.h> #include<stdlib.h> #define MAX_SIZE 100 // Structure for stack typedef struct { int arr[MAX_SIZE]; int top; } Stack; // Function to initialize stack void initStack(Stack *s) { s->top = -1; } // Function to check if stack is empty int isEmpty(Stack *s) { return s->top == -1; } // Function to push element onto stack void push(Stack *s, int value) { if (s->top >= MAX_SIZE - 1) { printf("Stack overflow"); exit(1); } s->top++; s->arr[s->top] = value; } // Function to pop element from stack int pop(Stack *s) { if (isEmpty(s)) { printf("Stack underflow"); exit(1); } int value = s->arr[s->top]; s->top--; return value; } // Function to reverse an array using a stack void reverseArrayStack(int *arr, int length) { Stack s; initStack(&s); for(int i=0;i<length;i++) { push(&s,arr[i]); } for(int i=0;i<length;i++) { arr[i]=pop(&s); } } int main() { int arr1[]={1,2,3,4,5}; int length1=5; int arr2[]={10,20,30,40,50,60,70,80,90}; int length2=9; printf("Original arrays:\n"); printf("arr1: "); for(int i=0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for(int i=0;i<length2;i++) { printf("%d",arr2[i]); } printf("\n"); reverseArrayStack(arr1,length1); reverseArrayStack(arr2,length2); printf("Reversed arrays:\n"); printf("arr1: "); for(int i=0;i<length1;i++) { printf("%d",arr1[i]); } printf("\narr2: "); for (int i=0;i<length2;i++) { printf("%d ",arr2[i]); } printf("\n"); return 0; }
Output:
Original arrays: arr1:1 2 3 4 5 arr2:10 20 30 40 50 60 70 80 90 Reversed arrays: arr1: 5 4 3 2 1 arr2: 90 80 70 60 50 40 30 20 10
All the algorithms successfully reversed the order of the elements in the arrays, but with different approaches. The pointers algorithm directly manipulated the elements of the array and while the stack algorithm utilized an additional data structure to reverse the order. The use of different algorithms to use depends on the specific requirements of the program, such as efficiency, memory usage, and complexity.