Wednesday, June 1, 2022
HomeWordPress DevelopmentCircularly Sorted Array (Sorted and Rotated Array)

Circularly Sorted Array (Sorted and Rotated Array)


Enhance Article

Save Article

Like Article

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++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool checkCircularSorted(int arr[], int n)

{

  

    

    

    int cnt = 0;

  

    for (int i = 1; i < n; i++) {

  

        

        if (arr[i - 1] > arr[i]) {

            cnt++;

        }

    }

  

    

    

    if (cnt > 1) {

        return false;

    }

    else {

        return true;

    }

}

  

int important()

{

  

    

    int arr[] = { 23, 34, 45, 12, 17, 19 };

  

    

    int N = sizeof(arr) / sizeof(arr[0]);

  

    

    

    bool outcome = checkCircularSorted(arr, N);

  

    

    if (outcome) {

        cout << "Array is circularly sorted";

    }

    else {

        cout << "Array will not be circularly sorted";

    }

}

Java

  

public class Foremost {

  

    

    

    public static boolean checkCircularSorted(int arr[])

    {

        

        

        int cnt = 0;

  

        for (int i = 1; i < arr.size; i++) {

            

            if (arr[i - 1] > arr[i]) {

                cnt++;

            }

        }

        

        

        if (cnt > 1) {

            return false;

        }

        else {

            return true;

        }

    }

  

    

    public static void important(String args[])

    {

        

        int arr[] = { 23, 34, 45, 12, 17, 19 };

  

        

        

        boolean outcome = checkCircularSorted(arr);

  

        

        if (outcome == true) {

            System.out.println(

                "Array is circularly sorted");

        }

        else {

            System.out.println(

                "Array will not be circularly sorted");

        }

    }

}

Output

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:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments