A very efficient and economical way to shift an array
2008-12-01 13:02
399 查看
How do you shift an array? For example, there is an array {a, b, c, d, 1, 2, 3, 4}, and the requirement is to cyclic-shift 3 elements to the right. That means, after the shift, the array looks like this: {2, 3, 4, a, b, c, d, 1}. This problem is so easy that most people can solve it in just one or two minutes, or even less time. However, here introduces another interesting way to shoot it, which uses reversing and is very economical in both time and space usage. The above example is used again to illustrate the whole process. Totally we need 2 times of reversing. The first time, we reverse the two subarrays with (n-k) and k elements respectively, that is {a, b, c, d, 1, 2, 3, 4} --> {1, d, c, b, a, 4, 3, 2}; the second time, we reverse the whole new array, that is {1, d, c, b, a, 4, 3, 2} --> {2, 3, 4, a, b, c, d, 1}. Job done. So interesting, efficient and economical, right? Below is one implementation.
#include <iostream>
using namespace std;
template <typename T>
void array_print(T* array, int n) {
for(int i = 0; i < n; ++i) {
cout << array[i] << " ";
}
cout << endl;
}
template <typename T>
T* array_shift(T* array, int n, int k) {
k %= n;
if(k == 0) {
return array;
}
int temp;
// Reverse the left subarray with (n-k) elements.
for(int i = 0; i < (n-k)/2; ++i) {
temp = array[i];
array[i] = array[n-k-1-i];
array[n-k-1-i] = temp;
}
// Reverse the right subarray with k elements.
for(int i = 0; i < k/2; ++i) {
temp = array[(n-k)+i];
array[(n-k)+i] = array[(n-1)-i];
array[(n-1)-i] = temp;
}
// Reverse the whole array.
for(int i = 0; i < n/2; ++i) {
temp = array[i];
array[i] = array[n-1-i];
array[n-1-i] = temp;
}
return array;
}
int main() {
int ia[10] = {1,3,5,7,9,2,4,6,8,10};
array_print(array_shift(ia, 10, 7), 10);
char ca[8] = {'a','b','c','d','1','2','3','4'};
array_print(array_shift(ca, 8, 4), 8);
array_print(array_shift(ca, 8, 4), 8);
array_print(array_shift(ca, 8, 5), 8);
return 0;
}
The command line prints:
7 9 2 4 6 8 10 1 3 5
1 2 3 4 a b c d
a b c d 1 2 3 4
d 1 2 3 4 a b c
Referenced to <Beauty of Programming>.
#include <iostream>
using namespace std;
template <typename T>
void array_print(T* array, int n) {
for(int i = 0; i < n; ++i) {
cout << array[i] << " ";
}
cout << endl;
}
template <typename T>
T* array_shift(T* array, int n, int k) {
k %= n;
if(k == 0) {
return array;
}
int temp;
// Reverse the left subarray with (n-k) elements.
for(int i = 0; i < (n-k)/2; ++i) {
temp = array[i];
array[i] = array[n-k-1-i];
array[n-k-1-i] = temp;
}
// Reverse the right subarray with k elements.
for(int i = 0; i < k/2; ++i) {
temp = array[(n-k)+i];
array[(n-k)+i] = array[(n-1)-i];
array[(n-1)-i] = temp;
}
// Reverse the whole array.
for(int i = 0; i < n/2; ++i) {
temp = array[i];
array[i] = array[n-1-i];
array[n-1-i] = temp;
}
return array;
}
int main() {
int ia[10] = {1,3,5,7,9,2,4,6,8,10};
array_print(array_shift(ia, 10, 7), 10);
char ca[8] = {'a','b','c','d','1','2','3','4'};
array_print(array_shift(ca, 8, 4), 8);
array_print(array_shift(ca, 8, 4), 8);
array_print(array_shift(ca, 8, 5), 8);
return 0;
}
The command line prints:
7 9 2 4 6 8 10 1 3 5
1 2 3 4 a b c d
a b c d 1 2 3 4
d 1 2 3 4 a b c
Referenced to <Beauty of Programming>.
相关文章推荐
- Algorithm: efficient way to remove duplicate integers from an array
- pointer to ref of an array, and to the func that return a ref of an array
- An easy way to install and uninstall .Net Windows Service
- An Efficient Way to Draw Approximate Circles in OpenGL
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- Best Way to Loop Through an Array in JavaScript
- If you had an array of 1 million values and you had to remove duplicates
- A very simple MFC class to Encode and Decode an url string
- how to remove nil and blank string in an array in Ruby
- An innovative way to replace AJAX and JSONP using node.js and socket.io
- Core - Provide an easy way to store administrator and user model differences in a custom store (e.g., in a database)
- Given an array of characters which form a sentence of words, give an efficient algorithm to reverse
- You’re given an array containing both positive and negative integers and required to find the sub-a
- 转载:how to automate Microsoft Excel and return the values from a multi-cell range to an array
- ToDoList-An effective and flexible way to keep on top of your tasks(ToDoList-管理任务的有效工具)
- Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses
- CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
- Call C# Code from C++ and read an array of struct which contains strings
- 面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
- Mac: An easy way to crack Sublime Text 2.0.2