From the course: C# Algorithms

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Solution: rotate an array

Solution: rotate an array - C# Tutorial

From the course: C# Algorithms

Solution: rotate an array

Let's create an algorithm that rotates the contents of an array to the left by one. We start off with an input array. After the rotation, the new array should contain 2 through 6 and then 1. Let's implement it. The first step is to create a new array with the same size as the input array. This will create a new array, the same size as numbers, but with each item set to 0. Next, we'll copy the elements from the numbers array to the rotated array, but at one index to the left. This means we'll want to set the item at index 0 of the rotated array to be the item at index 1 of the numbers array. We'll also want to set the item at index 1 of the rotated array to be the item at index 2 of the numbers array. And this pattern continues on with the next set until the end of the array. Seeing this pattern, we can set the item at index i-1 of the rotated array to be the item at index i of the numbers array. This shifts all the contents to the left and it'll work for most of our array elements…

Contents