https://www.geeksforgeeks.org/check-if-array-can-be-sorted-with-one-swap/
bool checkSorted(int n, int arr[])
{
// Create a sorted copy of original array
int b[n];
for (int i = 0; i < n; i++)
b[i] = arr[i];
sort(b, b + n);
// Check if 0 or 1 swap required to
// get the sorted array
int ct = 0;
for (int i = 0; i < n; i++)
if (arr[i] != b[i])
ct++;
if (ct == 0 || ct == 2)
return true;
else
return false;
}
https://www.geeksforgeeks.org/sort-an-almost-sorted-array-where-only-two-elements-are-swapped/
https://www.geeksforgeeks.org/check-given-array-almost-sorted-elements-one-position-away/
Given an array containing N elements. Find if it is possible to sort it in non-decreasing order using atmost one swap.
Examples:
Input : arr[] = {1, 2, 3, 4}
Output : YES
The array is already sortedInput : arr[] = {3, 2, 1}
Output : YES
Swap 3 and 1 to get [1, 2, 3]Input : arr[] = {4, 1, 2, 3}
Output :NO
An efficient solution is to check in linear time. Let us consider different cases that may appear after one swap.
- We swap adjacent elements. For example {1, 2, 3, 4, 5} becomes {1, 2, 4, 3, 5}
- We swap non-adjacent elements. For example {1, 2, 3, 4, 5} becomes {1, 5, 3, 4, 2}
We traverse the given array. For every element, we check if it is smaller than previous element. We count such occurrences. If count of such occurrences is more than 2, then we cannot sort the array with one swap. If count is one, we can find elements to swap (smaller and its previous).
If count is two, we can find elements to swap (previous of first smaller and second smaller).
After swapping, we again check if array becomes sorted or not. We check this to handle cases like {4, 1, 2, 3}
If count is two, we can find elements to swap (previous of first smaller and second smaller).
After swapping, we again check if array becomes sorted or not. We check this to handle cases like {4, 1, 2, 3}
A simple approach is to sort the array and compare the required position of the element and the current position of the element. If there are no mismatches, the array is already sorted. If there are exactly 2 mismatches, we can swap the terms that are not in position to get the sorted array.
{
// Create a sorted copy of original array
int b[n];
for (int i = 0; i < n; i++)
b[i] = arr[i];
sort(b, b + n);
// Check if 0 or 1 swap required to
// get the sorted array
int ct = 0;
for (int i = 0; i < n; i++)
if (arr[i] != b[i])
ct++;
if (ct == 0 || ct == 2)
return true;
else
return false;
}
https://www.geeksforgeeks.org/sort-an-almost-sorted-array-where-only-two-elements-are-swapped/
Given an almost sorted array where only two elements are swapped, how to sort the array efficiently?
Examples :
Input: arr[] = {10, 20, 60, 40, 50, 30} // 30 and 60 are swapped Output: arr[] = {10, 20, 30, 40, 50, 60} Input: arr[] = {10, 20, 40, 30, 50, 60} // 30 and 40 are swapped Output: arr[] = {10, 20, 30, 40, 50, 60} Input: arr[] = {1, 5, 3} // 3 and 5 are swapped Output: arr[] = {1, 3, 5}
Expected time complexity is O(n) and only one swap operation to fix the array.
The idea is to traverse from rightmost side and find the first out of order element (element which is smaller than previous element). Once first element is found, find the other our of order element by traversing the array toward left side.
Given an array with n distinct elements. An array is said to be almost sorted (non-decreasing) if any of its elements can occurs maximum of 1 distance away from their original places in sorted array. We need to find whether the given array is almost sorted or not.