Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 杂耍算法_Algorithm_Asymptotic Complexity_Greatest Common Divisor - Fatal编程技术网

Algorithm 杂耍算法

Algorithm 杂耍算法,algorithm,asymptotic-complexity,greatest-common-divisor,Algorithm,Asymptotic Complexity,Greatest Common Divisor,方法(杂耍算法) 将数组分成不同的集合,集合数等于n和d的GCD,并在集合内移动元素。 如果GCD与上述示例数组(n=7和d=2)一样为1,则元素将仅在一个集合内移动,我们只需从temp=arr[0]开始,并将arr[I+d]移动到arr[I],最后将temp存储在正确的位置 下面是n=12和d=3的示例。GCD为3,并且 设arr[]为{1,2,3,4,5,6,7,8,9,10,11,12} a) 元素首先在第一组中移动–(有关此移动,请参见下图) 排列 arr[] after t

方法(杂耍算法) 将数组分成不同的集合,集合数等于n和d的GCD,并在集合内移动元素。 如果GCD与上述示例数组(n=7和d=2)一样为1,则元素将仅在一个集合内移动,我们只需从temp=arr[0]开始,并将arr[I+d]移动到arr[I],最后将temp存储在正确的位置

下面是n=12和d=3的示例。GCD为3,并且

设arr[]为{1,2,3,4,5,6,7,8,9,10,11,12}

a) 元素首先在第一组中移动–(有关此移动,请参见下图)

排列

      arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b) 然后是第二盘。 arr[]在此步骤之后-->{4 5 3 7 8 6 10 11 9 1 2}

c) 最后进入第三盘。 arr[]在此步骤之后-->{4 5 6 7 8 9 10 11 12 1 2 3} /*函数打印数组*/ void printary(int arr[],int size)

获取a和b的gcd的函数*/ 国际通用数据表(国际通用数据表a、国际通用数据表b); /*函数将大小n的arr[]向左旋转d*/ void leftRotate(int arr[],int d,int n) { 内部i、j、k、温度; 对于(i=0;i=n) k=k-n; 如果(k==i) 打破 arr[j]=arr[k]; j=k; } arr[j]=温度; } } /*效用函数*/ /*函数打印数组*/ void printary(int arr[],int size) { int i; 对于(i=0;i 时间复杂度:O(n) 辅助空间:O(1)


有人能给我解释一下这个算法的工作原理及其渐进复杂性吗?

函数中的for循环:

leftRotate(int arr[], int d, int n)
将进行严格的
gcd(d,n)
迭代。现在让我们看看循环中发生了什么:它需要所有的单元格
arr[k]
来实现:
k%gcd(d,n)==i
然后交换它们。当然确实有:
n/gcd(d,n)
,这就是函数在循环的一次迭代中将进行多少次交换。因此,函数的整体渐近时间复杂度将是
O(gcd(d,n)*n/gcd(d,n))==O(n)
。代码的其余部分对时间复杂度没有影响,几乎是自我解释的。

杂耍算法

在这种方法中,将数组划分为M个集合,其中M=GCD(n,k),然后旋转每个集合中的元素

根据阵列的元素数(n)和要对阵列进行的旋转数(k),生成块的GCD(n,k)数。 然后,在每个块中,将对块中的相应元素进行移位

所有块中的所有元素移位后,数组将旋转给定次数

例如:如果要将下面的数组旋转2个位置。 123456

  M = GCD(6, 2) = 2;
  Initial Array : 1  2  3  4  5  6   
  First Set Moves : 5   2   1   4   3   6
  Second Set Moves : 5   6   1   2   3   4          //  The array is rotated twice.

public class Main
{

/*Fuction to get gcd of a and b*/
public static int gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    else
        return gcd(b, a % b); 
}

/*Function to left rotate array of by d number of rotations*/
public static void leftRotate(int arr[], int d, int n) 
{ 
    int i, j, k, temp; 
    for (i = 0; i < gcd(d, n); i++) // gcd(d,n) times the loop will iterate
    { 
        /* move i-th values of blocks */
        temp = arr[i]; 
        j = i; 
        while (true) { 
            k = j + d; 
            if (k >= n) // The element has to be shifted to its rotated position
                k = k - n; 
            if (k == i) // The element is already in its rotated position
                break; 
            arr[j] = arr[k]; 
            j = k; 
        } 
        arr[j] = temp; 
    }} 

//  Main function
public static void main(String[] args) 
{ 
    int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; 
    int no_of_rotations = 2;
    int n = arr.length;
    System.out.println("Array Elements before rotating : "); 
    for(int i = 0 ; i < n ; i++)
    {
        System.out.print(arr[i]+ " "); // Printing elements before rotation
    }
    leftRotate(arr, no_of_rotations, n); 
    System.out.println("\nArray Elements after rotating : "); 
    for(int i = 0 ; i < n ; i++)
    {
        System.out.print(arr[i] + " "); // Printing elements after rotation
} } }
M=GCD(6,2)=2;
初始阵列:123456
第一盘移动:521436
第二组移动:561234//阵列旋转两次。
公共班机
{
/*获得a和b的gcd的功能*/
公共静态整数gcd(整数a、整数b)
{ 
如果(b==0)
返回a;
其他的
返回gcd(b,a%b);
}
/*函数将数组左旋转d个旋转数*/
公共静态void leftRotate(int arr[],int d,int n)
{ 
内部i、j、k、温度;
对于(i=0;i=n)//必须将元素移动到其旋转位置
k=k-n;
if(k==i)//元素已处于其旋转位置
打破
arr[j]=arr[k];
j=k;
} 
arr[j]=温度;
}} 
//主要功能
公共静态void main(字符串[]args)
{ 
int arr[]={1,2,3,4,5,6,7};
int no_of_旋转=2;
int n=阵列长度;
System.out.println(“旋转前的数组元素:”);
对于(int i=0;i
  M = GCD(6, 2) = 2;
  Initial Array : 1  2  3  4  5  6   
  First Set Moves : 5   2   1   4   3   6
  Second Set Moves : 5   6   1   2   3   4          //  The array is rotated twice.

public class Main
{

/*Fuction to get gcd of a and b*/
public static int gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    else
        return gcd(b, a % b); 
}

/*Function to left rotate array of by d number of rotations*/
public static void leftRotate(int arr[], int d, int n) 
{ 
    int i, j, k, temp; 
    for (i = 0; i < gcd(d, n); i++) // gcd(d,n) times the loop will iterate
    { 
        /* move i-th values of blocks */
        temp = arr[i]; 
        j = i; 
        while (true) { 
            k = j + d; 
            if (k >= n) // The element has to be shifted to its rotated position
                k = k - n; 
            if (k == i) // The element is already in its rotated position
                break; 
            arr[j] = arr[k]; 
            j = k; 
        } 
        arr[j] = temp; 
    }} 

//  Main function
public static void main(String[] args) 
{ 
    int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; 
    int no_of_rotations = 2;
    int n = arr.length;
    System.out.println("Array Elements before rotating : "); 
    for(int i = 0 ; i < n ; i++)
    {
        System.out.print(arr[i]+ " "); // Printing elements before rotation
    }
    leftRotate(arr, no_of_rotations, n); 
    System.out.println("\nArray Elements after rotating : "); 
    for(int i = 0 ; i < n ; i++)
    {
        System.out.print(arr[i] + " "); // Printing elements after rotation
} } }