5 Best Ways to Check if It Is Possible to Sort the Array After Rotating It in Python

πŸ’‘ Problem Formulation: You are given an array of integers and the task is to determine whether it is possible to sort this array in non-decreasing order by performing a series of rotations on the array. A rotation includes taking any number of elements from the end of the array and moving them to the beginning. The desired output is a boolean value indicating the possibility of sorting the array through rotation.

Method 1: Brute Force Check All Rotations

The brute force method simply rotates the array by all possible lengths and checks if any of these rotations result in a sorted array. This is achieved by slicing the array and reconstructing it in all possible rotated forms, then testing for sort order.

Here’s an example:

def is_sortable_by_rotation(arr):
    n = len(arr)
    for i in range(n):
        if arr[i:] + arr[:i] == sorted(arr):
            return True
    return False

array = [3, 4, 5, 1, 2]

Output: True

This code snippet defines a function that takes an array, rotates it by each possible length, and then checks if the result matches the sorted version of the original array. If a match is found, it returns True; otherwise, after all rotations have been checked, it returns False.

Method 2: Find the Pivot

This method involves finding a pivot – a point in the array where the order switches from ascending to descending. Once the pivot is found, we can reposition the elements after the pivot to the front and compare with the sorted array.

Here’s an example:

def find_pivot(arr):
    for i in range(len(arr) - 1):
        if arr[i] > arr[i + 1]:
            return i
    return -1

def can_sort_by_rotating(arr):
    pivot = find_pivot(arr)
    if pivot == -1:
        return True
    return arr[pivot+1:] + arr[:pivot+1] == sorted(arr)

array = [3, 4, 5, 1, 2]

Output: True

The function find_pivot() locates the pivot point in the array. With this information, can_sort_by_rotating() reconstructs the array by rotating at the pivot point and checks if the array is sort-able. This method assumes there’s only one pivot.

Method 3: Using Python’s Built-in Methods

This method leverages Python’s built-in methods such as min(), which can help in identifying the pivot, enabling us to quickly find the position to rotate and check for a sorted array.

Here’s an example:

def can_sort_by_rotation_builtin(arr):
    min_val = min(arr)
    min_index = arr.index(min_val)
    return arr[min_index:] + arr[:min_index] == sorted(arr)

array = [3, 4, 5, 1, 2]

Output: True

By finding the minimum value and its index in the array, the function can_sort_by_rotation_builtin() effectively locates a pivot and reconstructs the array by potential rotation before comparing it to the sorted version of the array.

Method 4: Optimized Pivot Search

This optimized pivot search assumes that a sorted array post-rotation should have a single pivot point. The function observes the change in the array’s sequence and determines if only one such point exists.

Here’s an example:

def optimized_pivot_check(arr):
    pivot_found = False
    for i in range(1, len(arr)):
        if arr[i-1] > arr[i]:
            if pivot_found:
                return False
            pivot_found = True
    return not pivot_found or arr[0] >= arr[-1]

array = [3, 4, 5, 1, 2]

Output: True

This snippet looks for the pivot by checking if there’s more than one instance where a later element is less than its previous one. If there’s only none or one such instance, and the first element is larger than or equal to the last, the array is sortable through rotation.

Bonus One-Liner Method 5: List Comprehension and Built-ins

A one-liner approach combining list comprehension and built-in functions can check for sort-ability by rotation by concatenating array slices where the first part is the minimum slice.

Here’s an example:

array = [3, 4, 5, 1, 2]
print(sorted(array) in (array[i:] + array[:i] for i in range(len(array))))

Output: True

The one-liner uses a generator expression to create all the possible rotations and checks if the sorted array is in any of them. This is a concise and pythonic way to solve the problem but is not the most efficient one due to all rotations being generated.


  • Method 1: Brute Force Check All Rotations. Strengths: Simple and clear logic. Weaknesses: Inefficient with large arrays as it checks all possible rotations.
  • Method 2: Find the Pivot. Strengths: More efficient by identifying the pivot. Ideal for arrays with distinct rotation points. Weaknesses: Assumes only one pivot and can fail on already sorted arrays.
  • Method 3: Using Python’s Built-in Methods. Strengths: Concise and leverages efficient built-in methods. Weaknesses: May not handle all edge cases, especially with non-distinctive elements.
  • Method 4: Optimized Pivot Search. Strengths: Fast and only searches for one pivot. Succeeds where Method 2 could fail. Weaknesses: Assumes a specific array structure and might not detect unsortable arrays with specific patterns.
  • Method 5: One-Liner with List Comprehension and Built-ins. Strengths: Extremely concise. Weaknesses: Generating all rotations is inefficient and not memory-friendly for large arrays.