new test case

explanations about algorithm
This commit is contained in:
beradrian 2015-05-07 11:31:28 +03:00
parent 5b7608d154
commit e79a590e0c
2 changed files with 25 additions and 0 deletions

View File

@ -2650,6 +2650,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -2708,6 +2710,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -2766,6 +2770,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -2824,6 +2830,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -2882,6 +2890,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -2940,6 +2950,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -2997,6 +3009,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -3054,6 +3068,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;
@ -3112,6 +3128,8 @@ public class ArrayUtils {
if (offset < 0) { if (offset < 0) {
offset += n; offset += n;
} }
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) { while (n > 1 && offset > 0) {
int n_offset = n - offset; int n_offset = n - offset;

View File

@ -2484,6 +2484,13 @@ public class ArrayUtilsTest {
assertEquals(4, array[1]); assertEquals(4, array[1]);
assertEquals(1, array[2]); assertEquals(1, array[2]);
assertEquals(2, array[3]); assertEquals(2, array[3]);
array = new short[] {1, 2, 3, 4, 5};
ArrayUtils.shift(array, 2);
assertEquals(4, array[0]);
assertEquals(5, array[1]);
assertEquals(1, array[2]);
assertEquals(2, array[3]);
assertEquals(3, array[4]);
} }
@Test @Test