Program to Check if Given Array is Sorted or Not

Program to Check if Given Array is Sorted or Not

  • Write a program to Check if Given Array is Sorted or Not in C
  • Write a program to Check if Given Array is Sorted or Not in C++
  • Write a program to Check if Given Array is Sorted or Not in Python
  • Write a program to Check if Given Array is Sorted or Not in PHP
  • Write a program to Check if Given Array is Sorted or Not in Java
  • Write a program to Check if Given Array is Sorted or Not in Java Script
  • Write a program to Check if Given Array is Sorted or Not in C#

Explanation:

We must ascertain whether the array’s items are arranged in ascending or descending order in order to determine whether the array is sorted. If every element in an array is greater than or equal to the next element (in an ascending order) or less than or equal to the next element (in a descending order), the array is said to be sorted.

Steps:

  1. Input the Array and Its Size:
    • Take the array size (n) and the array elements as input.
  2. Check for Ascending Order:
    • Traverse the array from the first element to the second-to-last element.
    • For each pair of consecutive elements, check if the current element is less than or equal to the next element.
    • If any pair is found where the current element is greater than the next element, the array is not sorted in ascending order.
  3. Check for Descending Order:
    • Similarly, check if each element is greater than or equal to the next element.
    • If any pair is found where the current element is less than the next element, the array is not sorted in descending order.
  4. Return the Result:
    • If both checks pass (ascending or descending), the array is sorted.
    • If neither pass, the array is not sorted.

Program to Check if Given Array is Sorted or Not

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50}; // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int is_sorted = 1;  // Assume the array is sorted initially

    for (int i = 1; i < size; i++) {
        if (arr[i] < arr[i - 1]) {
            is_sorted = 0;  // Found an unsorted pair
            break;
        }
    }

    if (is_sorted) {
        printf("Array is sorted\n");
    } else {
        printf("Array is not sorted\n");
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40, 50}; // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    bool is_sorted = true;  // Assume the array is sorted initially

    for (int i = 1; i < size; i++) {
        if (arr[i] < arr[i - 1]) {
            is_sorted = false;  // Found an unsorted pair
            break;
        }
    }

    if (is_sorted) {
        cout << "Array is sorted" << endl;
    } else {
        cout << "Array is not sorted" << endl;
    }

    return 0;
}

arr = [10, 20, 30, 40, 50]  # Example array

is_sorted = all(arr[i] <= arr[i + 1] for i in range(len(arr) - 1))

if is_sorted:
    print("Array is sorted")
else:
    print("Array is not sorted")

<?php
$arr = array(10, 20, 30, 40, 50);  // Example array

$is_sorted = true;  // Assume the array is sorted initially

for ($i = 1; $i < count($arr); $i++) {
    if ($arr[$i] < $arr[$i - 1]) {
        $is_sorted = false;  // Found an unsorted pair
        break;
    }
}

if ($is_sorted) {
    echo "Array is sorted\n";
} else {
    echo "Array is not sorted\n";
}
?>

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};  // Example array
        boolean isSorted = true;  // Assume the array is sorted initially

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < arr[i - 1]) {
                isSorted = false;  // Found an unsorted pair
                break;
            }
        }

        if (isSorted) {
            System.out.println("Array is sorted");
        } else {
            System.out.println("Array is not sorted");
        }
    }
}

const arr = [10, 20, 30, 40, 50];  // Example array

const isSorted = arr.every((val, index, array) => index === 0 || array[index - 1] <= val);

if (isSorted) {
    console.log("Array is sorted");
} else {
    console.log("Array is not sorted");
}

using System;

class Program {
    static void Main() {
        int[] arr = {10, 20, 30, 40, 50};  // Example array
        bool isSorted = true;  // Assume the array is sorted initially

        for (int i = 1; i < arr.Length; i++) {
            if (arr[i] < arr[i - 1]) {
                isSorted = false;  // Found an unsorted pair
                break;
            }
        }

        if (isSorted) {
            Console.WriteLine("Array is sorted");
        } else {
            Console.WriteLine("Array is not sorted");
        }
    }
}

List of All Programs