C Program to check whether an array is already sorted in ascending order
Learn how to check whether a C array is already sorted in ascending order using one loop, adjacent comparisons, and early termination.
An array is sorted in ascending order when every element is greater than or equal to the element before it. Another name for this arrangement is non-decreasing order.
For example:
Sorted: -3 0 0 4 9
Not sorted: 1 3 2 5
The first array is sorted because its values never decrease. Repeated values such as 0, 0 are allowed. The second array is not sorted because 2 appears after 3.
C Program to Check Whether an Array Is Sorted in Ascending Order
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int isSorted = 1;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
for (int index = 1; index < size; index++) {
if (array[index] < array[index - 1]) {
isSorted = 0;
break;
}
}
if (isSorted) {
printf("The array is sorted in ascending order.\n");
} else {
printf("The array is not sorted in ascending order.\n");
}
return 0;
}
Sample Output for a Sorted Array
Enter the number of elements: 5
Enter 5 elements:
-3 0 0 4 9
The array is sorted in ascending order.
Sample Output for an Unsorted Array
Enter the number of elements: 5
Enter 5 elements:
1 3 2 5 8
The array is not sorted in ascending order.
How the Program Works
The variable isSorted begins with the value 1, which represents true. The program then compares every element, beginning at index 1, with its immediate predecessor:
for (int index = 1; index < size; index++) {
if (array[index] < array[index - 1]) {
isSorted = 0;
break;
}
}
For an array to remain sorted, each comparison must satisfy:
array[index] >= array[index - 1]
If the current value is smaller than the previous value, the ascending order has been broken. The program sets isSorted to 0 and stops checking because a single descending pair is enough to prove that the whole array is not sorted.
Dry Run for a Sorted Array
Consider the array 2 4 4 7 10:
| Index | Previous value | Current value | Is current smaller? | Result |
|---|---|---|---|---|
| 1 | 2 | 4 | No | Continue |
| 2 | 4 | 4 | No | Continue |
| 3 | 4 | 7 | No | Continue |
| 4 | 7 | 10 | No | Continue |
No descending pair is found, so isSorted remains 1 and the array is reported as sorted.
Dry Run for an Unsorted Array
Now consider 2 6 5 8 10:
| Index | Previous value | Current value | Is current smaller? | Result |
|---|---|---|---|---|
| 1 | 2 | 6 | No | Continue |
| 2 | 6 | 5 | Yes | Set isSorted to 0 and stop |
The remaining elements do not need to be checked. Even though 5 8 10 is ascending, the pair 6 5 proves that the complete array is not.
Why Are Adjacent Comparisons Enough?
It is not necessary to compare every element with every later element. If every adjacent pair is in the correct order, the ordering carries through the entire array.
For example, if:
a[0] <= a[1]
a[1] <= a[2]
a[2] <= a[3]
then it follows that a[0] <= a[1] <= a[2] <= a[3]. A single left-to-right pass is therefore sufficient.
Why Does the Loop Start at Index 1?
The loop compares the current element with array[index - 1]. At index 0, there is no previous element, and using array[-1] would access memory outside the array.
Starting at index 1 makes the first valid comparison:
array[1] with array[0]
The final comparison is between array[size - 1] and array[size - 2].
Are Duplicate Values Considered Sorted?
Yes. This program checks for non-decreasing order, which is the usual meaning of ascending order for arrays. Equal adjacent elements are valid:
1 2 2 2 5
The condition detects only a decrease:
array[index] < array[index - 1]
If the requirement is strictly increasing order, duplicates must also make the check fail. Change the condition to:
array[index] <= array[index - 1]
With that version, 1 2 2 5 is not strictly increasing because the two 2 values are equal.
What Happens with One Element?
An array containing one element is always sorted because there is no pair in the wrong order. For a size of 1, the checking loop does not execute, and isSorted remains 1.
Array: 42
Result: sorted
This program requires at least one element. Although an empty array is often considered sorted mathematically, accepting an empty input would require changing the size validation to allow 0.
Does It Work with Negative Numbers?
Yes. Integer comparison works the same way for negative, zero, and positive values:
-10 -5 -5 0 6
This array is sorted because each value is greater than or equal to the value before it.
Checking Is Different from Sorting
This program does not rearrange any elements. It only reports whether the current arrangement is already sorted.
- Checking order: Reads the array once and leaves it unchanged.
- Sorting: Moves elements into the required order using an algorithm such as bubble sort, insertion sort, or quicksort.
Checking first can avoid unnecessary work. For example, a program can skip an expensive sorting operation when the input is already ordered.
Why Use break?
The break statement provides early termination. Once a descending pair is found, later comparisons cannot make the whole array sorted again.
For this input:
9 1 2 3 4
the program stops after comparing 9 and 1. Without break, it would perform the remaining comparisons even though the final answer was already known.
Time and Space Complexity
For an array containing n elements:
- Best-case time complexity:
O(1)when the first pair is out of order and the loop exits immediately. - Worst-case time complexity:
O(n)when the array is sorted or the only decrease is near the end. - Extra space complexity:
O(1)because the check uses only the loop index and one flag, regardless of array size.
The input array itself uses O(n) storage, but that is input storage rather than extra working space used by the algorithm.
Common Mistakes
- Comparing every pair of elements and using an unnecessary nested loop.
- Starting the loop at index
0and attempting to readarray[-1]. - Using
>instead of<and accidentally checking for descending order. - Using
<=when duplicate values should be allowed. - Forgetting to initialize
isSortedto1before the loop. - Setting the flag on a decrease but forgetting that
breakcan stop unnecessary comparisons. - Sorting the array first, which makes the check meaningless because it changes the original order.
By comparing each value with its immediate predecessor, the program determines in one pass whether the array is already sorted in ascending order without modifying any element.