Python Program to right rotate elements of array


December 17, 2022, Learn eTutorial
1376

How to right rotate or shift elements in an array?

In this python program, we need to right-rotate the array. An array is a collection of elements of the same data type which is stored in sequential memory locations. We have to move each element n number of times to the right, where the n is given by the user.

For example, consider an array A with 5 elements: A = [ 1, 2, 3, 4, 5]. Suppose we have the value for n is 2, then we have to shift 2 times to the right.

  • In the first iteration, we will shift by one to right and append the last element of the array to the array's first position, and A will be like A = [ 5, 1, 2, 3, 4]
  • In the second iteration, we will shift again to the right once more and then append the last one to the first of the array. Then it will then be like A = [ 4, 5, 1, 2, 3]

How to implement the array elements right shift in Python?

So applying this logic in the python program, we use nested for loop in python. One outer for loop, which loops from zero to the value of n and assigns the variable last with the array's last element. Outer for loop is continued till how many times the user wants to rotate the elements to the right. Now we use the inner for loop in the reverse direction, which means, from the last element of the array minus one to zero decrementing by one and applying array[j] = array[j-1]. Then finally we append the value in last as the first element.

ALGORITHM

STEP 1: initialize an array with some predefined values in it.

STEP 2: Add value to n to determine how many times we have to right-shift the array elements.

STEP 3: Display the real array using the for loop and print statement in the python programming language.

STEP 4: Add the outer for loop from zero to n and apply a variable last with the value of the last element of the array.

STEP 5: Use the inner for loop from the end of the array minus one to zero decrementing one.

STEP 6: Assign the value of array[j] = arr[j-1] and append the first element from the variable last.

STEP 7: Display the array after the right shift using for loop in python.


To rotate the array elements in python, we need to use the below concepts, We recommend to learn those for better understanding

Python Source Code

                                          arr = [1, 2, 3, 4, 5];     
  
n = 3;    # value of how many times the array should be right shifted
     

print("Original array: ");    # display the real array
for i in range(0, len(arr)):    
    print(arr[i])     
     
    
for i in range(0, n):    
     
    last = arr[len(arr)-1];    # add the last element of array to a variable
        
    for j in range(len(arr)-1, -1, -1):    
            
        arr[j] = arr[j-1];    # use the for loop and assign the value array[j] = array[j-1]
            
     
    arr[0] = last;    # append the first element of array from the variable
    
print("\nArray after right rotation: ");    # print array after rotation
for i in range(0, len(arr)):    
    print(arr[i])  
                                      

OUTPUT

Original array: 
1
2
3
4
5

Array after right rotation: 
3
4
5
1
2