Skip to content

Commit 0fbd9eb

Browse files
cdjolemaibin
authored andcommitted
BAEL-3122 In-Place Sort (eugenp#7542)
* BAEL-3122 In-Place Sort * BAEL-3122 rename Sort main and test classes * BAEL-3122 Class renaming
1 parent 51c1b3d commit 0fbd9eb

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.algorithms.inoutsort;
2+
3+
public class InOutSort {
4+
5+
public static int[] reverseInPlace(int A[]) {
6+
int n = A.length;
7+
for (int i = 0; i < n / 2; i++) {
8+
int temp = A[i];
9+
A[i] = A[n - 1 - i];
10+
A[n - 1 - i] = temp;
11+
}
12+
13+
return A;
14+
}
15+
16+
public static int[] reverseOutOfPlace(int A[]) {
17+
int n = A.length;
18+
int[] B = new int[n];
19+
for (int i = 0; i < n; i++) {
20+
B[n - i - 1] = A[i];
21+
}
22+
23+
return B;
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.algorithms.inoutsort;
2+
3+
import static org.junit.Assert.*;
4+
import static org.junit.Assert.assertArrayEquals;
5+
6+
import org.junit.Test;
7+
8+
public class InOutSortUnitTest {
9+
10+
@Test
11+
public void givenArray_whenInPlaceSort_thenReversed() {
12+
int[] input = {1, 2, 3, 4, 5, 6, 7};
13+
int[] expected = {7, 6, 5, 4, 3, 2, 1};
14+
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseInPlace(input));
15+
}
16+
17+
@Test
18+
public void givenArray_whenOutOfPlaceSort_thenReversed() {
19+
int[] input = {1, 2, 3, 4, 5, 6, 7};
20+
int[] expected = {7, 6, 5, 4, 3, 2, 1};
21+
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseOutOfPlace(input));
22+
}
23+
}

0 commit comments

Comments
 (0)