📜  用于数组旋转的Python程序

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

用于数组旋转的Python程序

编写一个函数rotate(ar[], d, n) 将大小为 n 的 arr[] 旋转 d 个元素。

大批

将上述数组旋转 2 次将生成数组

数组旋转1

方法 1(使用临时数组)

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
1) Store d elements in a temp array
   temp[] = [1, 2]
2) Shift rest of the arr[]
   arr[] = [3, 4, 5, 6, 7, 6, 7]
3) Store back the d elements
   arr[] = [3, 4, 5, 6, 7, 1, 2]
Python3
# function to rotate array by d elements using temp array
def rotateArray(arr, n, d):
    temp = []
    i = 0
    while (i < d):
        temp.append(arr[i])
        i = i + 1
    i = 0
    while (d < n):
        arr[i] = arr[d]
        i = i + 1
        d = d + 1
    arr[:] = arr[: i] + temp
    return arr
 
 
# Driver function to test above function
arr = [1, 2, 3, 4, 5, 6, 7]
print("Array after left rotation is: ", end=' ')
print(rotateArray(arr, len(arr), 2))
 
# this code is contributed by Anabhra Tyagi


Python3
#Function to left rotate arr[] of size n by d*/
def leftRotate(arr, d, n):
    for i in range(d):
        leftRotatebyOne(arr, n)
 
#Function to left Rotate arr[] of size n by 1*/
def leftRotatebyOne(arr, n):
    temp = arr[0]
    for i in range(n-1):
        arr[i] = arr[i+1]
    arr[n-1] = temp
         
 
# utility function to print an array */
def printArray(arr,size):
    for i in range(size):
        print ("%d"% arr[i],end=" ")
 
  
# Driver program to test above functions */
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
printArray(arr, 7)
 
# This code is contributed by Shreyanshi Arun


Python3
#Function to left rotate arr[] of size n by d
def leftRotate(arr, d, n):
    for i in range(gcd(d,n)):
         
        # move i-th values of blocks
        temp = arr[i]
        j = i
        while 1:
            k = j + d
            if k >= n:
                k = k - n
            if k == i:
                break
            arr[j] = arr[k]
            j = k
        arr[j] = temp
 
#UTILITY FUNCTIONS
#function to print an array
def printArray(arr, size):
    for i in range(size):
        print ("%d" % arr[i], end=" ")
  
#Function to get gcd of a and b
def gcd(a, b):
    if b == 0:
        return a;
    else:
        return gcd(b, a%b)
  
# Driver program to test above functions
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
printArray(arr, 7)
 
# This code is contributed by Shreyanshi Arun


Python3
# Python program using the List
# slicing approach to rotate the array
def rotateList(arr,d,n):
  arr[:]=arr[d:n]+arr[0:d]
  return arr
# Driver function to test above function
arr = [1, 2, 3, 4, 5, 6]
print(arr)
print("Rotated list is")
print(rotateList(arr,2,len(arr))) 
 
# this code is contributed by virusbuddah


输出
Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]

时间复杂度: O(n)
辅助空间: O(d)

方法2(一个一个旋转)

leftRotate(arr[], d, n)
start
  For i = 0 to i < d
    Left rotate all elements of arr[] by one
end

要旋转 1,请将 arr[0] 存储在临时变量 temp 中,将 arr[1] 移动到 arr[0],将 arr[2] 移动到 arr[1] ...最后将 temp 移动到 arr[n-1]
让我们以同样的例子 arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
将 arr[] 旋转 2 次
我们在第一次旋转后得到 [2, 3, 4, 5, 6, 7, 1],在第二次旋转后得到 [3, 4, 5, 6, 7, 1, 2]。

Python3

#Function to left rotate arr[] of size n by d*/
def leftRotate(arr, d, n):
    for i in range(d):
        leftRotatebyOne(arr, n)
 
#Function to left Rotate arr[] of size n by 1*/
def leftRotatebyOne(arr, n):
    temp = arr[0]
    for i in range(n-1):
        arr[i] = arr[i+1]
    arr[n-1] = temp
         
 
# utility function to print an array */
def printArray(arr,size):
    for i in range(size):
        print ("%d"% arr[i],end=" ")
 
  
# Driver program to test above functions */
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
printArray(arr, 7)
 
# This code is contributed by Shreyanshi Arun
输出
3 4 5 6 7 1 2 

时间复杂度: O(n * d)
辅助空间: O(1)

方法 3(杂耍算法)
这是方法2的扩展。不是一个一个移动,而是将数组分成不同的集合
其中集合数等于 n 和 d 的 GCD,并在集合内移动元素。
如果 GCD 为 1,如上面的示例数组(n = 7 和 d =2),则元素将仅在一组内移动,我们只需从 temp = arr[0] 开始并继续移动 arr[I+d]到 arr[I] 并最终将 temp 存储在正确的位置。
这是 n = 12 和 d = 3 的示例。GCD 为 3 并且

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

a)    Elements are first moved in first set – (See below diagram for this movement

数组旋转

arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}

b)    Then in second set.
          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}

c)    Finally in third set.
          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}

Python3

#Function to left rotate arr[] of size n by d
def leftRotate(arr, d, n):
    for i in range(gcd(d,n)):
         
        # move i-th values of blocks
        temp = arr[i]
        j = i
        while 1:
            k = j + d
            if k >= n:
                k = k - n
            if k == i:
                break
            arr[j] = arr[k]
            j = k
        arr[j] = temp
 
#UTILITY FUNCTIONS
#function to print an array
def printArray(arr, size):
    for i in range(size):
        print ("%d" % arr[i], end=" ")
  
#Function to get gcd of a and b
def gcd(a, b):
    if b == 0:
        return a;
    else:
        return gcd(b, a%b)
  
# Driver program to test above functions
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
printArray(arr, 7)
 
# This code is contributed by Shreyanshi Arun
输出
3 4 5 6 7 1 2 

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

另一种方法:使用列表切片

Python3

# Python program using the List
# slicing approach to rotate the array
def rotateList(arr,d,n):
  arr[:]=arr[d:n]+arr[0:d]
  return arr
# Driver function to test above function
arr = [1, 2, 3, 4, 5, 6]
print(arr)
print("Rotated list is")
print(rotateList(arr,2,len(arr))) 
 
# this code is contributed by virusbuddah
输出
[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]

如果数组需要旋转超过其长度,则应进行 mod。

例如:将大小为 n 的 arr[] 旋转 d,其中 d 大于 n。在这种情况下,d%n 应该由 mod 后的结果计算和旋转。
有关更多详细信息,请参阅有关阵列旋转程序的完整文章!