Last active
April 14, 2017 14:47
-
-
Save DrPizza/32f365dccb9cd00d0fe2a88f5a516948 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// positive rotations go left, negative go right | |
void rotate(int arr[], const ptrdiff_t len, const ptrdiff_t m) { | |
if(0 == m || m >= len) { | |
return; | |
} | |
ptrdiff_t dst = 0; | |
ptrdiff_t first_in_cycle = 0; | |
int tmp = arr[first_in_cycle]; | |
for(ptrdiff_t i = 0; i < len; ++i) { | |
ptrdiff_t src = dst + m; | |
if(src < 0) { | |
src += len; | |
} | |
if(src >= len) { | |
src -= len; | |
} | |
if(src != first_in_cycle) { | |
arr[dst] = arr[src]; | |
dst = src; | |
} else { | |
arr[dst] = tmp; | |
dst = ++first_in_cycle; | |
tmp = arr[first_in_cycle]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment