data-structures · beginner · ~15 min
Verify an ordering invariant.
Check whether an array is sorted in non-decreasing order.
Implement int is_sorted(const int *a, int n) that returns 1 if the first n elements of a are in non-decreasing order, else 0.
a: array of at least n ints (read-only).n: the number of elements to check.int: 1 if a[0] <= a[1] <= ... <= a[n-1], else 0.
{1,2,2,3} -> 1 (equal neighbours allowed)
{1,3,2} -> 0
{5} -> 1
a: array of at least n ints; n: element count.
int: 1 if the array is non-decreasing, else 0.
Equal neighbours are allowed (non-decreasing).
int is_sorted(const int *a, int n) {
/* TODO */
return 1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.