
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if array can be sorted with one swap in Python
Sorting is a task used to organize the numbers in increasing order. Usually, we use the sorting algorithms to do this, but in most cases, the array is almost sorted, with two or three numbers in the wrong position.
In such scenarios, instead of sorting the entire array, we can check if swapping just one pair of elements will make the entire array sorted.
Checking if Array can be Sorted with One Swap
The given task is to check if an array can be sorted with one swap in Python. i.e., given an array with distinct integers, we need to find if it can become completely sorted by swapping only one pair of elements. Here, we are not performing the swap, just checking if such a swap is possible or not.
Scenario 1
Input: [1, 3, 2, 4, 5] Output: True Explanation: In this array if we swap 3 and 2 results in [1, 2, 3, 4, 5], which is sorted., hence only one swap is needed the function returns true.
Scenario 2
Input: [1, 5, 3, 2, 4] Output: False Explanation: In the given array, more than two elements are out of the place compared to the sorted array {1,2,3,4,5]. so the function returns false.
Let's dive into the following example to get a better understanding of checking whether the given array can be sorted with one swap or not.
Algorithm
The following is an algorithm to check if an array can be sorted with one swap in Python:
- Step 1: We create a new list, which is the sorted version of the original array.
- Step 2: Iterating through both the original array and the sorted array and collecting the indices where the elements differ.
- Step 3: If there are no mismatches found, we will return true(i.e array is sorted).
- Step 4: If exactly two indices are mismatched, we will try to fix the array with one swap, making it an equally sorted array.
- Step 5: If the array becomes equal to the sorted array after oenswap, we will return true; else, false.
Program to Check if an Array can be Sorted with One Swap
Let's look at the following example, where we are going to swap 3, 2 from the given array [1,3,2,4,5], which makes it sorted with one swap.
def demo(array): x = sorted(array) y = [] for i in range(len(array)): if array[i] != x[i]: y.append(i) if len(y) == 0: return True elif len(y) == 2: i, j = y array[i], array[j] = array[j], array[i] return array == x else: return False array = [1, 3, 2, 4, 5] print(demo(array))
Following is the output of the above program:
True
Conclusion
Checking if an array can be sorted with one swap helps to detect mismatches in a sequence. Instead of applying complex sorting algorithms, this approach helps in identifying whether just two misplaced elements can be corrected with a single swap.