📌  相关文章
📜  查找数组是否可以通过限制为 k 倍数的交换进行排序

📅  最后修改于: 2021-09-06 05:26:45             🧑  作者: Mango

给定一个数组和一个数字 k,任务是检查给定的数组是否可以通过有限的交换操作进行排序。我们只能将 arr[i] 与 arr[i] 或 arr[i + k] 或 arr[i + 2*k] 或 arr[i + 3*k] 等交换。通常,索引 i 处的元素可以与索引 i + j*k 处的元素交换,其中 j = 0, 1, 2, 3, …
注意:可以在阵列上执行任意数量的交换。

例子:

方法:
1) 创建 sortArr[] 作为给定 arr 的排序版本。
2) 将此数组与已排序数组进行比较。
3) 迭代 for 循环,比较索引 i。
4) 现在索引 i,元素与
指数 = i + j * k
其中 j = 0, 1, 2 …..
5) 如果 sortArr[i] 的特定 i 元素与序列 arr[index] 匹配,则标志为 1 并且
交换 arr[i], arr[index]
6) 如果没有交换,则标志为 0,这意味着没有找到序列中的元素
7) 如果标志为 0 则中断循环并打印 不可能
8) else 打印可能

C++14
#include 
using namespace std;
 
// CheckSort function
// To check if array can be sorted
void CheckSort(vector arr,int k,int n){
 
    // sortarr is sorted array of arr
    vector sortarr(arr.begin(),arr.end());
 
    sort(sortarr.begin(),sortarr.end());
 
    // if k = 1 then (always possible to sort)
    // swapping can easily give sorted
    // array
    if (k == 1)
        printf("yes");
    else
    {
        int flag = 0;
         
        // comparing sortarray with array
        for (int i = 0; i < n; i++)
        {
            flag = 0;
 
            // element at index j
            // must be in j = i + l * k form
            // where i = 0, 1, 2, 3...
            // where l = 0, 1, 2, 3, ..n-1
            for (int j = i; j < n; j += k)
            {
 
                //if element is present
                //then swapped
                if (sortarr[i] == arr[j]){
                    swap(arr[i], arr[j]);
                    flag = 1;
                    break;
                }
                if (j + k >= n)
                    break;
 
            }
 
 
            // if element of sorted array
            // does not found in its sequence
            // then flag remain zero
            // that means arr can not be
            // sort after swapping
            if (flag == 0)
                break;
 
            }
 
        // if flag is 0
        // Not possible
        // else Possible
        if (flag == 0)
            printf("Not possible to sort");
        else
            printf("Possible to sort");
        }
}
 
 
// Driver code
int main()
{
    // size of step
    int k = 3;
 
    // array initialized
    vector arr ={1, 5, 6, 9, 2, 3, 5, 9};
 
    // length of arr
    int n =arr.size();
 
    // calling function
    CheckSort(arr, k, n);
 
    return 0;
}
 
// This code is contributed by mohit kumar 29


Java
import java.util.*;
 
class GFG{
     
// CheckSort function
// To check if array can be sorted
public static void CheckSort(Vector arr,
                             int k, int n)
{
     
    // sortarr is sorted array of arr
    Vector sortarr = new Vector();
    for(int i = 0; i < arr.size(); i++)
    {
        sortarr.add(arr.get(i));
    }
  
    Collections.sort(sortarr);
  
    // If k = 1 then (always possible to sort)
    // swapping can easily give sorted
    // array
    if (k == 1)
        System.out.println("yes");
    else
    {
        int flag = 0;
          
        // Comparing sortarray with array
        for(int i = 0; i < n; i++)
        {
            flag = 0;
  
            // Element at index j
            // must be in j = i + l * k form
            // where i = 0, 1, 2, 3...
            // where l = 0, 1, 2, 3, ..n-1
            for(int j = i; j < n; j += k)
            {
                 
                // If element is present
                //then swapped
                if (sortarr.get(i) == arr.get(j))
                {
                    Collections.swap(arr, i, j);
                    flag = 1;
                    break;
                }
                if (j + k >= n)
                    break;
            }
  
            // If element of sorted array
            // does not found in its sequence
            // then flag remain zero
            // that means arr can not be
            // sort after swapping
            if (flag == 0)
                break;
        }
         
        // If flag is 0
        // Not possible
        // else Possible
        if (flag == 0)
            System.out.println("Not possible to sort");
        else
            System.out.println("Possible to sort");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Size of step
    int k = 3;
  
    // Array initialized
    Vector arr = new Vector();
    arr.add(1);
    arr.add(5);
    arr.add(6);
    arr.add(9);
    arr.add(2);
    arr.add(3);
    arr.add(5);
    arr.add(9);
  
    // Length of arr
    int n = arr.size();
  
    // Calling function
    CheckSort(arr, k, n);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# CheckSort function
# To check if array can be sorted
def CheckSort(arr, k, n):
     
    # sortarr is sorted array of arr
    sortarr = sorted(arr)
     
    # if k = 1 then (always possible to sort)
    # swapping can easily give sorted
    # array
    if (k == 1):
        print("yes")
    else:
         
        # comparing sortarray with array
        for i in range(0, n):
            flag = 0
             
            # element at index j
            # must be in j = i + l * k form
            # where i = 0, 1, 2, 3...
            # where l = 0, 1, 2, 3, ..n-1
            for j in range(i, n, k):
 
                # if element is present
                # then swapped
                if (sortarr[i] == arr[j]):
                    arr[i], arr[j] = arr[j], arr[i]
                    flag = 1
                    break
                if (j + k >= n):
                    break
 
            # if element of sorted array
            # does not found in its sequence
            # then flag remain zero
            # that means arr can not be
            # sort after swapping
            if (flag == 0):
                break
             
        # if flag is 0
        # Not possible
        # else Possible
        if (flag == 0):
            print("Not possible to sort")
        else:
            print("Possible to sort")
 
 
# Driver code
if __name__ == "__main__":
    # size of step
    k = 3
 
    # array initialized
    arr =[1, 5, 6, 9, 2, 3, 5, 9]
 
    # length of arr
    n = len(arr)
 
    # calling function
    CheckSort(arr, k, n)


C#
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
   
// CheckSort function
// To check if array can be sorted
static void CheckSort(ArrayList arr, int k, int n)
{
     
    // sortarr is sorted array of arr
    ArrayList sortarr = new ArrayList(arr);
    sortarr.Sort();
     
    // If k = 1 then (always possible to sort)
    // swapping can easily give sorted
    // array
    if (k == 1)
        Console.Write("yes");
    else
    {
        int flag = 0;
          
        // Comparing sortarray with array
        for(int i = 0; i < n; i++)
        {
            flag = 0;
             
            // Element at index j
            // must be in j = i + l * k form
            // where i = 0, 1, 2, 3...
            // where l = 0, 1, 2, 3, ..n-1
            for(int j = i; j < n; j += k)
            {
                 
                // If element is present
                // then swapped
                if ((int)sortarr[i] == (int)arr[j])
                {
                    int tmp = (int)arr[i];
                    arr[i] = (int)arr[j];
                    arr[j] = tmp;
                    flag = 1;
                    break;
                }
                 
                if (j + k >= n)
                {
                    break;
                }
            }
 
            // If element of sorted array
            // does not found in its sequence
            // then flag remain zero
            // that means arr can not be
            // sort after swapping
            if (flag == 0)
            {
                break;
            }
        }
         
        // If flag is 0
        // Not possible
        // else Possible
        if (flag == 0)
            Console.Write("Not possible to sort");
        else
            Console.Write("Possible to sort");
    }
}
 
// Driver code
public static void Main(string[] args)
{
     
    // Size of step
    int k = 3;
  
    // Array initialized
    ArrayList arr = new ArrayList(){ 1, 5, 6, 9,
                                     2, 3, 5, 9 };
  
    // Length of arr
    int n = arr.Count;
  
    // Calling function
    CheckSort(arr, k, n);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
Possible to sort

性能分析:
时间复杂度: O(N^2) 其中 N 是数组的大小。最糟糕的情况
辅助空间: O(N),其中 N 是数组的大小。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live