📜  检查给定数组是否成对排序

📅  最后修改于: 2022-05-13 01:57:49.975000             🧑  作者: Mango

检查给定数组是否成对排序

如果每对连续的数字都按排序(非递减)顺序排列,则数组被认为是成对排序的。如果是奇数元素,则忽略最后一个元素,结果基于剩余的偶数个元素。

例子:

Input : arr[] = {10, 15, 9, 9, 1, 5};
Output : Yes
Pairs are (10, 15), (9,  9) and (1, 5).
All these pairs are sorted in non-decreasing
order.

Input : arr[] = {10, 15, 8, 9, 10, 5};
Output : No
The last pair (10, 5) is not sorted.

这个想法是从左到右遍历数组。成对比较元素,如果任何对违反属性,我们返回 false。如果没有对违反属性,我们返回 true。

C++
// CPP program to check if an array is pair wise
// sorted.
#include 
using namespace std;
 
// Check whether the array is pairwise sorted
// or not.
bool checkPairWiseSorted(int arr[], int n)
{
    if (n == 0 || n == 1)
        return true;
    for (int i = 0; i < n; i += 2)
        if (arr[i] > arr[i + 1])
            return false;
    return true;
}
 
// Driver program to test above function
int main()
{
   int arr[] = {2, 5, 3, 7, 9, 11};
   int n = sizeof(arr) / sizeof(arr[0]);  
   if (checkPairWiseSorted(arr, n))
       printf("Yes");
   else
       printf("No");      
   return 0;
}


Java
// java program to check if an array
// is pair wise sorted.
 
import java.io.*;
 
public class GFG {
     
    // Check whether the array is
    // pairwise sorted or not.
    static boolean checkPairWiseSorted(
                          int []arr, int n)
    {
        if (n == 0 || n == 1)
            return true;
             
        for (int i = 0; i < n; i += 2)
            if (arr[i] > arr[i + 1])
                return false;
                 
        return true;
    }
     
    // Driver program to test above
    // function
    static public void main (String[] args)
    {
        int []arr = {2, 5, 3, 7, 9, 11};
        int n = arr.length;
         
        if (checkPairWiseSorted(arr, n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by vt_m.


Python
# Python code to check whether the array
# is pairwise sorted or not.
def checkPairWiseSorted(a, n):
     
    if n == 0 or n == 1:
        return True
         
    for i in range(0, n, 2):
        if a[i] > a[i + 1]:
            return False
     
    return True
 
# Driver code
a = [2, 5, 3, 7, 9, 11]
n = len(a)
 
if checkPairWiseSorted(a, n):
    print "Yes"
else:
    print "No"
 
# This code is contributed by 'striver'.


C#
// C# program to check if an array is
// pair wise sorted.
using System;
 
public class GFG {
     
    // Check whether the array is
    // pairwise sorted or not.
    static bool checkPairWiseSorted(
                      int []arr, int n)
    {
        if (n == 0 || n == 1)
            return true;
             
        for (int i = 0; i < n; i += 2)
            if (arr[i] > arr[i + 1])
                return false;
                 
        return true;
    }
     
    // Driver program to test above
    // function
    static public void Main ()
    {
        int []arr = {2, 5, 3, 7, 9, 11};
        int n = arr.Length;
         
        if (checkPairWiseSorted(arr, n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");    
    }
}
 
// This code is contributed by vt_m.


PHP
 $arr[$i + 1])
            return false;
    return true;
}
 
// Driver program to test above function
$arr = array(2, 5, 3, 7, 9, 11);
$n = count($arr);
 
if (checkPairWiseSorted($arr, $n))
    echo "Yes";
else
    echo "No";
     
// This code is contributed by anuj_67.
?>


Javascript


输出:

Yes

时间复杂度: O(n)
空间复杂度: O(1)