Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to check if elements to the left and right of the pivot are smaller or greater respectively
In Python, a pivot is an element used to partition data into two groups: elements smaller than the pivot on the left, and elements greater than the pivot on the right. This concept is commonly used in sorting algorithms like quicksort.
Let's explore different methods to check if elements to the left of a pivot are smaller and elements to the right are greater than the pivot value.
Using Loops
We can iterate through the array using loops to check each element against the pivot value ?
def check_pivot_with_loops(arr, pivot_index):
pivot = arr[pivot_index]
# Check left elements are smaller
for i in range(pivot_index):
if arr[i] >= pivot:
return False
# Check right elements are greater
for i in range(pivot_index + 1, len(arr)):
if arr[i] <= pivot:
return False
return True
# Test with valid pivot
numbers = [1, 2, 3, 4, 5]
pivot_index = 2
if check_pivot_with_loops(numbers, pivot_index):
print("Valid pivot: Left elements are smaller, right elements are greater")
else:
print("Invalid pivot arrangement")
Valid pivot: Left elements are smaller, right elements are greater
Using List Slicing
List slicing creates sublists that we can validate using builtin functions ?
def check_pivot_with_slicing(arr, pivot_index):
pivot = arr[pivot_index]
left = arr[:pivot_index]
right = arr[pivot_index + 1:]
# Check all left elements are smaller and all right elements are greater
return all(x < pivot for x in left) and all(x > pivot for x in right)
# Test with invalid pivot
numbers = [1, 3, 3, 4, 5]
pivot_index = 2
if check_pivot_with_slicing(numbers, pivot_index):
print("Valid pivot arrangement")
else:
print("Invalid pivot: Elements don't satisfy the condition")
print(f"Pivot value: {numbers[pivot_index]}")
print(f"Left elements: {numbers[:pivot_index]}")
print(f"Right elements: {numbers[pivot_index + 1:]}")
Invalid pivot: Elements don't satisfy the condition Pivot value: 3 Left elements: [1, 3] Right elements: [4, 5]
Using max() and min() Functions
We can use builtin functions to find maximum of left elements and minimum of right elements ?
def check_pivot_with_minmax(arr, pivot_index):
pivot = arr[pivot_index]
# Handle edge cases
if pivot_index == 0:
return len(arr) == 1 or min(arr[1:]) > pivot
if pivot_index == len(arr) - 1:
return max(arr[:pivot_index]) < pivot
# Check using max/min
left_max = max(arr[:pivot_index])
right_min = min(arr[pivot_index + 1:])
return left_max < pivot < right_min
# Test different scenarios
test_cases = [
([1, 2, 5, 6, 7], 2), # Valid pivot
([3, 1, 4, 6, 2], 2), # Invalid pivot
([5], 0) # Single element
]
for arr, pivot_idx in test_cases:
result = check_pivot_with_minmax(arr, pivot_idx)
print(f"Array: {arr}, Pivot index: {pivot_idx}, Valid: {result}")
Array: [1, 2, 5, 6, 7], Pivot index: 2, Valid: True Array: [3, 1, 4, 6, 2], Pivot index: 2, Valid: False Array: [5], Pivot index: 0, Valid: True
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Loops | O(n) | O(1) | Memory efficiency |
| List Slicing | O(n) | O(n) | Readable code |
| max()/min() | O(n) | O(1) | Concise logic |
Conclusion
All three methods effectively validate pivot arrangements with O(n) time complexity. Use loops for memory efficiency, slicing for readability, or max/min functions for concise code. The choice depends on your specific requirements and coding style preferences.
