How to Check if an Array is Sorted

Search for a command to run...

No comments yet. Be the first to comment.
Finding the second largest element in an array can be approached in multiple ways. Here, we'll discuss three methods: using sorting, a better approach with two linear traversals and an optimal single-pass approach. Solution 1: Sorting One straightfor...
Finding the element that appears only once in an array where all other elements appear twice is a common problem in coding interviews and programming challenges. In this article, we'll discuss four approaches to solve this problem: one using a brute ...

Finding the maximum number of consecutive 1's in a binary array is a common problem that can be efficiently solved with a linear time algorithm. In this article, we will discuss an optimal approach to solve this problem. Solution: Optimal Approach Th...

In an array containing numbers from 1 to N, where one number is missing, the goal is to find the missing number. Here, we'll discuss four different methods to achieve this, ranging from brute force to optimal approaches. Solution 1: Brute Force Appro...

Finding the intersection of two sorted arrays is a common problem in coding interviews and programming challenges. In this article, we'll discuss two approaches to solve this problem: one using a brute force approach and another using two-pointers te...

Finding the union of two sorted arrays involves combining the elements of both arrays without duplicates. This problem can be solved using different approaches, each with its own time and space complexity. In this article, we'll discuss two approache...

There are many times we need to check if an array is sorted or not. Checking if an array is sorted can be approached in multiple ways. Here, we we'll discuss two solutions: a brute force approach and an optimal approach.
This method involves comparing each element with every other element that comes after it in the array to ensure that the array is sorted in non-decreasing order.
Implementation:
// Solution-1: Brute Force Approach
// Time Complexity: O(n*n)
// Space Complexity: O(1)
bool isArraySorted(vector<int> &arr, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[i])
return false;
}
}
return true;
}
Logic:
Nested Loops: Use two nested loops to compare each element with every subsequent element in the array.
Check Order: If any element is found to be greater than a subsequent element, the array is not sorted and the function returns false.
Return True: If no such pair is found, the array is sorted and the function returns true.
Time Complexity: O(n²)
n times and for each iteration, the inner loop runs up to n-1 times, resulting in a quadratic time complexity.Space Complexity: O(1)
Example:
Input: arr = [10, 20, 30, 40, 50], n = 5
Output: true
Explanation: All elements are in non-decreasing order.
A more efficient method involves a single pass through the array, comparing each element with its predecessor to ensure that the array is sorted.
Implementation:
// Solution-2: Optimal Approach
// Time Complexity: O(n)
// Space Complexity: O(1)
bool isArraySorted(vector<int> &arr, int n)
{
for (int i = 1; i < n; i++)
{
if (arr[i] < arr[i - 1])
{
return false;
}
}
return true;
}
Logic:
Single Loop: Traverse the array starting from the second element.
Compare with Predecessor: For each element, check if it is less than its predecessor.
Return False: If any element is found to be less than its predecessor, the array is not sorted and the function returns false.
Return True: If no such element is found, the array is sorted and the function returns true.
Time Complexity: O(n)
Space Complexity: O(1)
Example:
Input: arr = [10, 20, 30, 40, 50], n = 5
Output: true
Explanation: All elements are in non-decreasing order.
Brute Force Method:
Optimal Method:
Empty Array: An empty array is considered sorted.
Single Element Array: An array with a single element is considered sorted.
Array with All Identical Elements: An array where all elements are the same is considered sorted.
Efficiency: The optimal approach is significantly more efficient for large datasets.
Simplicity: Despite its efficiency, the optimal approach is also simple to implement.
Practicality: The optimal method is generally preferred due to its linear time complexity and constant space complexity.
Checking if an array is sorted can be done efficiently using a single-pass approach. While the brute force method provides a simple but inefficient solution, the optimal method is both efficient and easy to implement, making it suitable for large datasets.