Algorithm and Flowchart for Left Rotation on an Array

[2160 views]




An array is a data structure containing elements of the same data type. These elements are stored in contiguous locations in the memory. Hence, we can easily find out the position of each element of the array by adding the base address with an offset value. Offset is the difference between two indexes of the array and the base address is the memory location of the first element of the array. The elements of an array can be accessed through the it’s index. For example if we want to access the second element of an array, we can write: a[1]. Here the index the required variable in the array ‘a’ is 1.
Note: The index of an array always starts with 0.

What is Left Rotation of an array:

When we perform left rotation on an array, it shifts each element of the array to its left by one unit. The first element is shifted to the last and this is followed for all the other elements. Hence, this rotation happens in a circular way for the number of times it is specified by the user.
Let us consider an example:
Example 1:
A[] = { 1, 6, 7, 4, 8 }
After performing 4 rotations, the array will become:
A[] = { 8, 1, 6, 7, 4 }
Example 2:
Arr[] = { 7, 21, 15, 40, 23, 55, 45, 32, 80 }
After performing 5 rotations, the array will become:
Arr[] = { 55, 45, 32, 80, 7, 21, 15, 40, 23 }
Let us have a look at the algorithm and flowchart to perform left rotation on an array for specified number of times.

Algorithm to perform Left Rotation on an Array:

Step 1. Start Step 2. Read the array from the user, say ‘A[]’ Step 3. Read the number of rotations, say ‘r’ Step 4. Calculate the length of the array, say ‘l’ Step 5. Initialize loop variable, i = 0 Step 6: Repeat while i < r: 6.1: Store the first element of the array, f = a[0] 6.2: Initialize loop variable, j = 0 6.3: Repeat while j < (l - 1): 6.3.a: a[j] = a[j + 1] 6.3.b: Increment j by 1 6.4: a[j] = f 6.5: Increment i by 1 Step 7: Display the array Step 8: Stop

Explanation:

To start off the algorithm, we first take the array as input from the user. Then, we take the number of times the array must be rotated, as input from the user and store it in a variable say ‘r’. Now, we calculate the length of the array and store it in a variable say ‘l’.

We now start a loop to perform the left rotation. The loop runs from 0 to r, that is, the loop will run the number of times the array must be rotated. In each iteration, we store the first element of the array in a variable ‘f’ as this rotation is done in a circular manner. Now, we start another loop which runs from 0 to the length of the array. In this loop, we shift the elements of the array towards left, one by one by performing: a[j] = a[j+1]. After the completion of this loop, the first element of the array is shifted to the last position by: a[j] = f.

Once the first loop completes all the iterations, the resultant array is left rotated. We can now display this array.

Let us consider an example:
A[] = { 1, 2, 3, 4, 5 }
r = 4
l = 5
Here the loop will run 4 times.
After first iteration,
A[] = { 2, 3, 4, 5, 1 }
After second iteration,
A[] = { 3, 4, 5, 1, 2 }
After third iteration,
A[] = { 4, 5, 1, 2, 3 }
After fourth iteration,
A[] = { 5, 1, 2, 3, 4 }

Flowchart for Left Rotation on an array:

Algorithm and Flowchart to left rotation on an array
Remove WaterMark from Above Flowchart


                 



Having Difficulty understanding above Algorithm and Flowchart? Buy my Personal Notes



Are You Good enough in Algorithms? Prove it!



Comments





Search
Get Answers to your Programming Questions


Recommended Deals ends in








Test Your Skills:

Search Tags

    Algorithm to left rotate an array

    Flowchart to left rotate an array

    How to perform left rotation on an array

    Left Rotation of Array Algorithm