📌  相关文章
📜  检查是否可以通过条件交换允许的相邻数组进行排序

📅  最后修改于: 2021-05-04 07:56:13             🧑  作者: Mango

我们得到了一个从0到n-1范围内的未排序整数数组。我们可以多次交换数组中的相邻元素,但前提是这些元素之间的绝对差为1。检查是否可以对数组进行排序。如果是,则打印“是”,否则打印“否”。

例子:

Input : arr[] = {1, 0, 3, 2}
Output : yes
Explanation:- We can swap arr[0] and arr[1].
Again we swap arr[2] and arr[3]. 
Final arr[] = {0, 1, 2, 3}.

Input : arr[] = {2, 1, 0}
Output : no

尽管乍一看这些问题看起来很复杂,但是有一个简单的解决方案。如果我们从左到右遍历数组,并确保在到达i之前对索引i之前的元素进行排序,那么就必须在i之前具有最大值arr [0..i-1]。并且该最大值必须小于arr [i]或仅大于arr [i]。在第一种情况下,我们只是继续前进。在第二种情况下,我们交换并继续前进。

比较当前元素与数组中的下一个元素。如果当前元素大于下一个元素,请执行以下操作:-
…a)检查两个数字之间的差是否为1,然后将其交换。
…b)否则返回false。
如果到达数组末尾,则返回true。

C++
// C++ program to check if we can sort
// an array with adjacent swaps allowed
#include
using namespace std;
  
// Returns true if it is possible to sort
// else false/
bool checkForSorting(int arr[], int n)
{
    for (int i=0; i arr[i+1])
        {
            if (arr[i] - arr[i+1] == 1)
                swap(arr[i], arr[i+1]);
  
            // If difference is more than
            // one, then not possible
            else
                return false;
        }
    }
    return true;
}
  
// Driver code
int main()
{
    int arr[] = {1,0,3,2};
    int n = sizeof(arr)/sizeof(arr[0]);
    if (checkForSorting(arr, n))
       cout << "Yes";
    else
       cout << "No";
}


Java
class Main
{
    // Returns true if it is possible to sort
    // else false/
    static boolean checkForSorting(int arr[], int n)
    {
        for (int i=0; i arr[i+1])
            {
                if (arr[i] - arr[i+1] == 1)
                    {
                        // swapping
                        int temp = arr[i];
                        arr[i] = arr[i+1];
                        arr[i+1] = temp;
                    }
       
                // If difference is more than
                // one, then not possible
                else
                    return false;
            }
        }
        return true;
    }
      
    // Driver function
    public static void main(String args[])
    {
        int arr[] = {1,0,3,2};
        int n = arr.length;
        if (checkForSorting(arr, n))
           System.out.println("Yes");
        else
           System.out.println("No");
    }
}


Python3
# Python 3 program to
# check if we can sort
# an array with adjacent
# swaps allowed
  
# Returns true if it
# is possible to sort
# else false/
def checkForSorting(arr, n):
  
    for i in range(0,n-1):
      
        # We need to do something only if
        # previousl element is greater
        if (arr[i] > arr[i+1]):
          
            if (arr[i] - arr[i+1] == 1):
                arr[i], arr[i+1] = arr[i+1], arr[i]
  
            # If difference is more than
            # one, then not possible
            else:
                return False
  
    return True
  
# Driver code
arr = [1,0,3,2]
n = len(arr)
if (checkForSorting(arr, n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to check if we can sort
// an array with adjacent swaps allowed
using System;
  
class GFG
{
    // Returns true if it is 
    // possible to sort else false
    static bool checkForSorting(int []arr, int n)
    {
        for (int i=0; i arr[i+1])
            {
                if (arr[i] - arr[i+1] == 1)
                    {
                        // swapping
                        int temp = arr[i];
                        arr[i] = arr[i+1];
                        arr[i+1] = temp;
                    }
      
                // If difference is more than
                // one, then not possible
                else
                    return false;
            }
        }
        return true;
    }
      
    // Driver function
    public static void Main()
    {
        int []arr = {1, 0, 3, 2};
        int n = arr.Length;
        if (checkForSorting(arr, n))
        Console.Write("Yes");
        else
        Console.Write("No");
    }
}
  
// This code is contributed by nitin mittal.


PHP
 $arr[$i + 1])
        {
            if ($arr[$i] - $arr[$i + 1] == 1)
                {
                        // swapping
                        $temp = $arr[$i];
                        $arr[$i] = $arr[$i + 1];
                        $arr[$i + 1] = $temp;
                }
  
            // If difference is more than
            // one, then not possible
            else
                return false;
        }
    }
    return true;
}
  
    // Driver Code
    $arr = array(1,0,3,2);
    $n = sizeof($arr);
    if (checkForSorting($arr, $n))
        echo "Yes";
    else
        echo "No";
  
// This code is contributed
// by nitin mittal.
?>


输出:

Yes

时间复杂度= O(n)