Circularly sorted arrays are arrays which can be sorted in ascending or descending order after which rotated by quite a few steps.
Allow us to take an instance to know extra about circularly sorted arrays:
Contemplate an array: arr[] = {23, 34, 45, 12, 17, 19}
The weather right here, {12, 17, 19, 23, 34, 45} are sorted ‘In-order’ however they’re rotated to the left by 3 occasions.
Ascending circularly sorted array:
- Contemplate an array sorted in ascending order: arr[] = {12, 17, 19, 23, 34, 45}
- If the above array is rotated by 3 steps to the left, then the resultant array can be ascending circularly sorted array: {23, 34, 45, 12, 17, 19}
Descending circularly sorted array:
- Contemplate an array sorted in descending order: arr[] = {35, 26, 11, 9, 5, 3}
- If the above array is rotated by 3 steps to the left, then the resultant array can be descending circularly sorted array: {9, 5, 3, 35, 26, 11}
Allow us to verify by means of the beneath drawback whether or not the given array is circularly sorted or not:
Downside Assertion:
Given an array arr[] of size N, the duty is to verify whether or not the given array is circularly sorted or not, we have to verify whether or not the given array is the rotated type of the sorted array.
- In ascending circularly sorted array, there can be at most one case the place the ingredient simply earlier than the present ingredient can be better than the present ingredient i.e., arr[ i – 1 ] > arr[ i ]. So we have to depend the whole existence of such circumstances and if the depend is larger than 1 then the outcome can be false else the outcome can be true which means that the array is circularly sorted.
Beneath is the implementation for the above strategy:
C++
|
Java
|
Array is circularly sorted
Equally, we will do the above operation for descending circularly sorted array. On this case we have to take into account the depend of the case the place, arr [i-1] < arr[i].
Associated articles: