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
C# Program to Check if an Array is Sorted
We are given an array, and we need to check whether the array is sorted or not. The array can be sorted in either ascending or descending order. In this article, we will discuss different approaches to check if an array is sorted using C#.
Problem Examples
Input: array = {1, 2, 3, 4, 5}
Output: True
Explanation: The array is sorted in ascending order.
Input: array = {29, 28, 27, 26, 25, 24}
Output: True
Explanation: The array is sorted in descending order.
Input: array = {12, 10, 19, 11, 9}
Output: False
Explanation: The array is neither sorted in ascending nor descending order.
Using Iterative Approach
The iterative approach compares each element with the next element to determine if the array is sorted. We check for both ascending and descending order simultaneously using boolean flags
using System;
class Program {
static bool IsSortedIterative(int[] array) {
if (array.Length <= 1) return true;
bool isAscending = true, isDescending = true;
for (int i = 0; i < array.Length - 1; i++) {
if (array[i] > array[i + 1]) isAscending = false;
if (array[i] < array[i + 1]) isDescending = false;
if (!isAscending && !isDescending) return false;
}
return isAscending || isDescending;
}
static void Main() {
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 29, 28, 27, 26, 25, 24 };
int[] array3 = { 12, 10, 19, 11, 9 };
Console.WriteLine("Array1 is sorted: " + IsSortedIterative(array1));
Console.WriteLine("Array2 is sorted: " + IsSortedIterative(array2));
Console.WriteLine("Array3 is sorted: " + IsSortedIterative(array3));
}
}
The output of the above code is
Array1 is sorted: True Array2 is sorted: True Array3 is sorted: False
Time Complexity: O(n) we traverse the array once.
Space Complexity: O(1) constant space.
Using Recursive Approach
The recursive approach checks if the array is sorted by recursively comparing adjacent elements. We create separate methods for ascending and descending order checks
using System;
class Program {
static bool IsSortedAscending(int[] array, int index) {
if (index == array.Length - 1) return true;
if (array[index] > array[index + 1]) return false;
return IsSortedAscending(array, index + 1);
}
static bool IsSortedDescending(int[] array, int index) {
if (index == array.Length - 1) return true;
if (array[index] < array[index + 1]) return false;
return IsSortedDescending(array, index + 1);
}
static bool IsSortedRecursive(int[] array) {
if (array.Length <= 1) return true;
return IsSortedAscending(array, 0) || IsSortedDescending(array, 0);
}
static void Main() {
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 29, 28, 27, 26, 25, 24 };
int[] array3 = { 12, 10, 19, 11, 9 };
Console.WriteLine("Array1 is sorted: " + IsSortedRecursive(array1));
Console.WriteLine("Array2 is sorted: " + IsSortedRecursive(array2));
Console.WriteLine("Array3 is sorted: " + IsSortedRecursive(array3));
}
}
The output of the above code is
Array1 is sorted: True Array2 is sorted: True Array3 is sorted: False
Time Complexity: O(n) we check each element once.
Space Complexity: O(n) recursive call stack for n elements.
Using LINQ Approach
The LINQ approach compares the original array with sorted versions using SequenceEqual, OrderBy, and OrderByDescending methods
using System;
using System.Linq;
class Program {
static bool IsSortedLinq(int[] array) {
if (array.Length <= 1) return true;
bool isAscending = array.SequenceEqual(array.OrderBy(x => x));
bool isDescending = array.SequenceEqual(array.OrderByDescending(x => x));
return isAscending || isDescending;
}
static void Main() {
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 29, 28, 27, 26, 25, 24 };
int[] array3 = { 12, 10, 19, 11, 9 };
Console.WriteLine("Array1 is sorted: " + IsSortedLinq(array1));
Console.WriteLine("Array2 is sorted: " + IsSortedLinq(array2));
Console.WriteLine("Array3 is sorted: " + IsSortedLinq(array3));
}
}
The output of the above code is
Array1 is sorted: True Array2 is sorted: True Array3 is sorted: False
Time Complexity: O(n log n) due to the sorting operations.
Space Complexity: O(n) temporary storage for sorted arrays.
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best Use Case |
|---|---|---|---|
| Iterative | O(n) | O(1) | Most efficient for large arrays |
| Recursive | O(n) | O(n) | Educational purposes, functional style |
| LINQ | O(n log n) | O(n) | Readable code, small arrays |
Conclusion
The iterative approach is the most efficient solution with O(n) time and O(1) space complexity. The recursive approach offers the same time complexity but uses additional stack space. The LINQ approach provides the most readable code but has higher time complexity due to sorting operations.
