给定一个具有n个不同元素的数组。如果某个数组中的任何元素离其在已排序数组中的原始位置的最大距离为1,则该数组几乎被排序(不递减)。我们需要查找给定的数组是否几乎已排序。
例子:
Input : arr[] = {1, 3, 2, 4}
Output : Yes
Explanation : All elements are either
at original place or at most a unit away.
Input : arr[] = {1, 4, 2, 3}
Output : No
Explanation : 4 is 2 unit away from
its original place.
排序方法:借助排序,我们可以预测给定数组是否几乎已排序。其背后的思想是首先对输入数组说A []进行排序,然后如果将对数组进行几乎排序,则给定数组的每个元素Ai必须等于已排序数组B [的Bi-1,Bi或Bi + 1中的任何一个。 ]。
时间复杂度:O(nlogn)
// suppose B[] is copy of A[]
sort(B, B+n);
// check first element
if ((A[0]!=B[0]) && (A[0]!=B[1]) )
return 0;
// iterate over array
for(int i=1; i
时间复杂度:O(n Log n)
高效方法:该想法基于气泡排序。像冒泡排序一样,我们比较相邻的元素,如果它们不按顺序交换它们。在交换之后,我们在这里将索引再移动一个位置,以便将冒泡限制在一个位置。因此,经过一轮迭代后,如果对结果数组进行了排序,则可以说我们的输入数组几乎已排序,否则就几乎未排序。
// perform bubble sort tech once
for (int i=0; i
C++
// CPP program to find whether given array
// almost sorted or not
#include
using namespace std;
// function for checking almost sort
bool almostSort(int A[], int n)
{
// One by one compare adjacents.
for (int i = 0; i < n - 1; i++) {
if (A[i] > A[i + 1]) {
swap(A[i], A[i + 1]);
i++;
}
}
// check whether resultant is sorted or not
for (int i = 0; i < n - 1; i++)
if (A[i] > A[i + 1])
return false;
// is resultant is sorted return true
return true;
}
// driver function
int main()
{
int A[] = { 1, 3, 2, 4, 6, 5 };
int n = sizeof(A) / sizeof(A[0]);
if (almostSort(A, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// JAVA Code to check if given array is almost
// sorted or not
import java.util.*;
class GFG {
// function for checking almost sort
public static boolean almostSort(int A[], int n)
{
// One by one compare adjacents.
for (int i = 0; i < n - 1; i++) {
if (A[i] > A[i + 1]) {
int temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
i++;
}
}
// check whether resultant is sorted or not
for (int i = 0; i < n - 1; i++)
if (A[i] > A[i + 1])
return false;
// is resultant is sorted return true
return true;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int A[] = { 1, 3, 2, 4, 6, 5 };
int n = A.length;
if (almostSort(A, n))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 program to find whether given
# array almost sorted or not
# Function for checking almost sort
def almostSort(A, n):
# One by one compare adjacents.
i = 0
while i < n - 1:
if A[i] > A[i + 1]:
A[i], A[i + 1] = A[i + 1], A[i]
i += 1
i += 1
# check whether resultant is sorted or not
for i in range(0, n - 1):
if A[i] > A[i + 1]:
return False
# Is resultant is sorted return true
return True
# Driver Code
if __name__ == "__main__":
A = [1, 3, 2, 4, 6, 5]
n = len(A)
if almostSort(A, n):
print("Yes")
else:
print("No")
# This code is contributed
# by Rituraj Jain
C#
// C# Code to check if given array
// is almost sorted or not
using System;
class GFG {
// function for checking almost sort
public static bool almostSort(int []A, int n)
{
// One by one compare adjacents.
for (int i = 0; i < n - 1; i++)
{
if (A[i] > A[i + 1])
{
int temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
i++;
}
}
// Check whether resultant is
// sorted or not
for (int i = 0; i < n - 1; i++)
if (A[i] > A[i + 1])
return false;
// is resultant is sorted return true
return true;
}
// Driver Code
public static void Main()
{
int []A = {1, 3, 2, 4, 6, 5};
int n = A.Length;
if (almostSort(A, n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Nitin Mittal.
PHP
$A[$i + 1])
{
list($A[$i],
$A[$i + 1]) = array($A[$i + 1],
$A[$i] );
$i++;
}
}
// check whether resultant
// is sorted or not
for ($i = 0; $i <$n - 1; $i++)
if ($A[$i] > $A[$i + 1])
return false;
// is resultant is
// sorted return true
return true;
}
// Driver Code
$A = array (1, 3, 2,
4, 6, 5);
$n = sizeof($A) ;
if (almostSort($A, $n))
echo "Yes", "\n";
else
echo "Yes", "\n";
// This code is contributed by ajit
?>
输出:
Yes