How to rotate an integer array by i
times using swap
function only in linear time.
public static String rotateKTimes(String str,int k){
int n = str.length();
//java substring has O(n) complexity
return str.substring(n-k) + str.substring(0,n-k);
}
This algorithm does (at most) len(array)-1
swaps, and works with both positive (right) and negative (left) rotation amounts. The array is modified in-place.
It doesn't require calculating the GCD, unlike other similar methods.
(Python 3)
def rotate(array,amount):
if amount<0:
amount+=len(array)
a=0
b=0
for _ in range(len(array)-1):
b=(b+amount) % len(array)
if b==a:
a+=1
b=a
if b!=a:
array[a],array[b] = array[b],array[a] #swap array[a],array[b]
return array
When one cycle is not enough (if it returns to the start before reaching every index), start a new cycle, from the next index.
Note: rotating items a, b, c, d, ... can be done using
swap a,b
swap a,c
swap a,d
...
using only swap, following is a C++ implementation
template<class T>
void rotate_array(std::vector<T> *array, int i) {
int n = array->size();
i = i % n;
int gcd_n_i = gcd(i, n);
for (int j = 0; j < gcd_n_i; j++) {
T first_element = array->at(j);
for (int k = j; (k + i) % n != j; k = (k + i) % n) {
std::swap(array->at(k), array->at((k + i) % n));
}
}
}
You can read more about it at http://pointer-overloading.blogspot.in/2013/09/algorithms-rotating-one-dimensional.html
void rotate(int array [], int n, int k)
{
int temp1,temp2; //two variable to hold values
int visited,count,index; //'visited' to check if cycle
// collides with index
//'count' to check number of loops
visited = n-1;
temp1 = array[n-1];
index = n-1;
count = 0;
while(count<n)
{
temp2 = temp1;
temp1 = array[(n+index-k)%n];
array[(n+index-k)%n] = temp2;
index = (n+index-k)%n;
if (index == visited)
{
cout<<"\nindex == visited at index = "<<index;
getch();
index--;
visited=index;
temp1=array[index];
}
count++;
cout<<"\ncount is : "<<count;
cout<<"\narray is : ";
p_all_arr(array,0,n); //display arr
getch();
}
}
Here's a small snippet thats works in O(n), written in JavaScript. The keyconcept is, that you always have to work with the replaced item.
function swap(arr, a, v) {
var old = arr[a];
arr[a] = v;
return old;
}
function rotate(arr, n) {
var length = arr.length;
n = n % length;
if(!n) return arr;
for(var cnt = 0,
index = 0,
value = arr[index],
startIndex = index;
cnt < length;
cnt++) {
// Calc next index
var nextIndex = mapIndex(index, n, length);
// Swap value with next
value = swap(arr, nextIndex, value)
if(nextIndex == startIndex) {
startIndex = index = mapIndex(index, 1, length);
value = arr[index];
} else {
index = nextIndex;
}
}
return arr;
}
function mapIndex(index, n, length) {
return (index - n + length) % length;
}
console.log(rotate([1,2,3,4,5,6,7,8,9], 5))
console.log(rotate([1,2,3,4,5,6], 2))
Better use a direct and simple function, complexity N:
int rotate(int* a,int DIM,int rn,int* b) {
int i; //counter
for(i=0;i<DIM;i++){ // looping through the array
b[(i+rn)%len]=a[i]; // copying the values in the b array=a shifted with rn(+ for right or - for left shifting
}