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:
parent
51c1b3d575
commit
0fbd9ebca0
@ -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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user