Sitemap
Pooja G

Pooja G

Helluo Liborium , Travel Ethusiastic

C Program: Check Array Order (Ascending, Descending, or Unsorted)

3 min readDec 14, 2024

--

geeksforgeeks.org
#include <stdio.h>

// Function to check if the array is sorted in ascending order
int isAscending(int arr[], int n) {
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i-1]) {
return 0; // Returns 0 if the array is not in ascending order
}
}
return 1; // Returns 1 if the array is in ascending order
}

// Function to check if the array is sorted in descending order
int isDescending(int arr[], int n) {
for (int i = 1; i < n; i++) {
if (arr[i] > arr[i-1]) {
return 0; // Returns 0 if the array is not in descending order
}
}
return 1; // Returns 1 if the array is in descending order
}

// Main function to check the array order
void checkArrayOrder(int arr[], int n) {
// Check if the array is in ascending order
if (isAscending(arr, n)) {
printf("The array is in Ascending order.\n");
}
// Check if the array is in descending order
else if (isDescending(arr, n)) {
printf("The array is in Descending order.\n");
}
// If neither, the array is unsorted
else {
printf("The array is Unsorted.\n");
}
}

int main() {
// Test Case 1: Array in ascending order
int arr1[] = {1, 2, 3, 4, 5};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
printf("Test Case 1:\n");
checkArrayOrder(arr1, n1);
printf("\n");

// Test Case 2: Array in descending order
int arr2[] = {9, 8, 7, 6, 5};
int n2 = sizeof(arr2) / sizeof(arr2[0]);
printf("Test Case 2:\n");
checkArrayOrder(arr2, n2);
printf("\n");

// Test Case 3: Array in unsorted order
int arr3[] = {3, 5, 2, 8, 1};
int n3 = sizeof(arr3) / sizeof(arr3[0]);
printf("Test Case 3:\n");
checkArrayOrder(arr3, n3);
printf("\n");

// Test Case 4: Array with repeated elements, still ascending
int arr4[] = {1, 2, 2, 4, 5};
int n4 = sizeof(arr4) / sizeof(arr4[0]);
printf("Test Case 4:\n");
checkArrayOrder(arr4, n4);
printf("\n");

// Test Case 5: Array with all identical elements (sorted but constant)
int arr5[] = {7, 7, 7, 7, 7};
int n5 = sizeof(arr5) / sizeof(arr5[0]);
printf("Test Case 5:\n");
checkArrayOrder(arr5, n5);
printf("\n");

// Test Case 6: Array with only one element (always sorted)
int arr6[] = {5};
int n6 = sizeof(arr6) / sizeof(arr6[0]);
printf("Test Case 6:\n");
checkArrayOrder(arr6, n6);
printf("\n");

return 0;
}

Explanation:

  1. isAscending Function:This function iterates through the array and checks if each element is less than or equal to the next one. If any element is greater than the next, it returns 0, indicating the array is not in ascending order.

2. isDescending Function: Similar to isAscending, but this checks if each element is greater than or equal to the next. If any element is less than the next, it returns 0, indicating the array is not in descending order.

Get Pooja G’s stories in your inbox

Join Medium for free to get updates from this writer.

checkArrayOrder Function:This function calls isAscending and isDescending to determine if the array is in ascending, descending, or unsorted order. It then prints the result.

Test Cases:

  1. Array in Ascending Order: {1, 2, 3, 4, 5}
    The program will detect it as ascending order.
  2. Array in Descending Order: {9, 8, 7, 6, 5}
    The program will detect it as descending order.
  3. Unsorted Array: {3, 5, 2, 8, 1}
    The program will detect it as unsorted.
  4. Array with Repeated Elements (Ascending): {1, 2, 2, 4, 5}
    The program will still detect this as ascending order because repeated elements don’t violate ascending order.
  5. Array with Identical Elements: {7, 7, 7, 7, 7}
    This is treated as sorted (ascending) because all elements are equal.
  6. Single Element Array: {5}
    Any single-element array is trivially sorted in ascending order.
Test Case 1:
The array is in Ascending order.

Test Case 2:
The array is in Descending order.

Test Case 3:
The array is Unsorted.

Test Case 4:
The array is in Ascending order.

Test Case 5:
The array is in Ascending order.

Test Case 6:
The array is in Ascending order.

--

--

Helluo Liborium , Travel Ethusiastic