Check if Array is Sorted (Descending)

Check if array is sorted in descending order.

Logic BuildingAdvanced
Logic Building
# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Element {i+1}: ")))

# Check sorted descending
is_sorted = True
for i in range(len(arr) - 1):
    if arr[i] < arr[i + 1]:
        is_sorted = False
        break

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

Output

Enter array size: 5
Element 1: 9
Element 2: 7
Element 3: 5
Element 4: 3
Element 5: 1
Array is sorted in descending order

Check if arr[i] >= arr[i+1] for all i.

Key Concepts:

  • Compare each element with next
  • Check descending order
  • Early exit on violation