Skip to content

Instantly share code, notes, and snippets.

@whoo24
Created April 8, 2013 02:46
Show Gist options
  • Save whoo24/5333854 to your computer and use it in GitHub Desktop.
Save whoo24/5333854 to your computer and use it in GitHub Desktop.
배열 회전시키기 rotate array to right
#include <iostream>
void right_rotate(int arr[], int s, int t)
{
int i, last;
last = arr[t];
for(i = t; i > s; i--)
{
arr[i] = arr[i - 1];
}
arr[s] = last;
}
void right_rotate2(int arr[], int s, int t)
{
int i;
int len = t - s + 1;
int *d = (int *)malloc(sizeof(int) * len);
memcpy(d, &arr[s], sizeof(int)*len);
for(i = 0; i < len; i++)
{
arr[ s + (i + 1) % len] = d[i];
}
}
int main()
{
int arr[] = {
1, 2, 3, 4, 5, 6, 7, 8,
};
//right_rotate(arr, 2, 5);
for(int i = 0; i < 8; ++i)
{
std::cout << arr[i];
}
std::cout << std::endl;
right_rotate2(arr, 2, 5);
for(int i = 0; i < 8; ++i)
{
std::cout << arr[i];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment