How to check whether an array is sorted in C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>

bool isSorted(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        if (arr[i] > arr[i + 1]) {
            return false;
        }
    }
    
    return true;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int n = sizeof(arr) / sizeof(arr[0]);

    if (isSorted(arr, n)) {
        printf("The array is sorted\n");
    } else {
        printf("The array is not sorted\n");
    }

    return 0;
}



/*
run:

The array is sorted

*/

 



answered Mar 7, 2025 by avibootz
...