Problem Statement: Given an array of integers, write a C program to reverse the elements of the array in place.
Note: In-place means without using any additional data structure to store the reverse elements. The original array is updated to contain its elements in reverse order, without creating a new array to hold the reversed elements.
Example:
Input: arr[] = {10, 20, 30, 40, 50} Output: Reversed array: {50, 40, 30, 20, 10}
Algorithm to Reverse Array Elements.
Below are the steps that we need to follow:
Step 1: Take the size of the array and create an array arr of that size.
Step 2: Input the elements of the array arr.
Step 3: Initialize two variables, start with 0 and end to the size of the array minus 1.
Step 4: Swap the elements at start and end indexes and increment start and decrement end until the start becomes greater than or equal to the end.
Step 5: Print the reversed array.
Program to Reverse the Elements of an Array.
// C program to reverse array elements in-place #include <stdio.h> // Function to swap two elements void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int main() { int n; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; // Input the elements of the array printf("Enter the elements of the array:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } int start = 0; int end = n - 1; while (start < end) { swap(&arr[start], &arr[end]); start++; end--; } printf("Reversed array: {"); for (int i = 0; i < n; i++) { printf("%d", arr[i]); if (i < n - 1) { printf(", "); } } printf("}\n"); return 0; }
Enter the size of the array: 5
Enter the elements of the array:
11 10 9 20 8
Reversed array: {8, 20, 9, 10, 11}
Time Complexity: The time complexity of this code is O(n) because we need to traverse through the entire array once to reverse the elements.
Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to solve this problem.
Reverse the Elements of the Array Using Recursion.
We can also reverse the order of the elements stored in the given array by using the Recursive method.
Algorithm:
1. Base Case: If the array is empty or has only one element, it is already reversed.
2. Recursive Case:
- Swap the first and last elements of the array.
- Recursively reverse the remaining subarray.
C Program to Reverse Array Using Recursion.
// C code to reverse order of an array using recursive method #include <stdio.h> // Function to swap two elements void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // Recursive function to reverse an array void reverseArray(int arr[], int start, int end) { // Base case: if the array is empty or has only one element if (start >= end) { return; } // Swap the first and last elements swap(&arr[start], &arr[end]); // Recursively reverse the remaining subarray reverseArray(arr, start + 1, end - 1); } // Function to print an array void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original array: "); printArray(arr, n); // Reverse the array reverseArray(arr, 0, n - 1); printf("Reversed array: "); printArray(arr, n); return 0; }
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
- Time Complexity: O(n)
- Space Complexity: O(1)
No comments:
Post a Comment