BAEL-3122 In-Place Sort (#7542)

* BAEL-3122 In-Place Sort

* BAEL-3122 rename Sort main and test classes

* BAEL-3122 Class renaming
This commit is contained in:
cdjole 2019-08-11 07:46:49 +02:00 committed by maibin
parent 51c1b3d575
commit 0fbd9ebca0
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.baeldung.algorithms.inoutsort;
public class InOutSort {
public static int[] reverseInPlace(int A[]) {
int n = A.length;
for (int i = 0; i < n / 2; i++) {
int temp = A[i];
A[i] = A[n - 1 - i];
A[n - 1 - i] = temp;
}
return A;
}
public static int[] reverseOutOfPlace(int A[]) {
int n = A.length;
int[] B = new int[n];
for (int i = 0; i < n; i++) {
B[n - i - 1] = A[i];
}
return B;
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.algorithms.inoutsort;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class InOutSortUnitTest {
@Test
public void givenArray_whenInPlaceSort_thenReversed() {
int[] input = {1, 2, 3, 4, 5, 6, 7};
int[] expected = {7, 6, 5, 4, 3, 2, 1};
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseInPlace(input));
}
@Test
public void givenArray_whenOutOfPlaceSort_thenReversed() {
int[] input = {1, 2, 3, 4, 5, 6, 7};
int[] expected = {7, 6, 5, 4, 3, 2, 1};
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseOutOfPlace(input));
}
}