Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check if an array is sorted or not (Iterative and Recursive) in C
Given an array arr[] with n number of elements, our task is to check whether the given array is in sorted order or not. If it is in sorted order then print "The array is in sorted order", else print "The array is not in sorted order".
To solve this problem we can use iterative or recursive approach, we will be discussing both.
Syntax
int isSorted(int arr[], int n);
Method 1: Iterative Approach
In iterative approach, we use loops to traverse the array and compare adjacent elements ?
#include <stdio.h>
int isSorted(int arr[], int n) {
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i-1]) {
return 0; // Not sorted
}
}
return 1; // Sorted
}
int main() {
int arr[] = {12, 13, 14, 16, 18};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("
");
if (isSorted(arr, n)) {
printf("The array is in sorted order
");
} else {
printf("The array is not in sorted order
");
}
return 0;
}
Array: 12 13 14 16 18 The array is in sorted order
Method 2: Recursive Approach
In recursive approach we recursively call a function to check if the array is sorted by examining one element at a time ?
#include <stdio.h>
int isSortedRecursive(int arr[], int n) {
// Base case: single element or empty array is sorted
if (n == 1 || n == 0) {
return 1;
}
// Check if current element is smaller than previous
if (arr[n-1] < arr[n-2]) {
return 0;
}
// Recursively check remaining elements
return isSortedRecursive(arr, n-1);
}
int main() {
int arr[] = {2, 1, 3, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("
");
if (isSortedRecursive(arr, n)) {
printf("The array is in sorted order
");
} else {
printf("The array is not in sorted order
");
}
return 0;
}
Array: 2 1 3 5 6 The array is not in sorted order
Key Points
- The iterative approach has O(n) time complexity and O(1) space complexity.
- The recursive approach has O(n) time complexity and O(n) space complexity due to function call stack.
- Both approaches compare adjacent elements to determine if the array is sorted.
- An array with 0 or 1 elements is considered sorted.
Comparison
| Method | Time Complexity | Space Complexity | Pros | Cons |
|---|---|---|---|---|
| Iterative | O(n) | O(1) | Memory efficient, no stack overflow risk | More code for loop handling |
| Recursive | O(n) | O(n) | Clean and elegant code | Uses extra memory, stack overflow for large arrays |
Conclusion
Both iterative and recursive approaches effectively check if an array is sorted. The iterative method is preferred for large arrays due to better space efficiency, while the recursive approach offers cleaner code structure.
