[JAVA-616] core-java-arrays-sorting
* Creation * Moved code from https://www.baeldung.com/java-sorting-arrays * Moved code from https://www.baeldung.com/java-check-sorted-array * Moved code from https://www.baeldung.com/java-invert-array * Moved code from https://www.baeldung.com/java-arrays-sort-vs-parallelsort
This commit is contained in:
@@ -10,7 +10,4 @@ This module contains articles about Java arrays
|
||||
- [Multi-Dimensional Arrays In Java](https://www.baeldung.com/java-jagged-arrays)
|
||||
- [Find Sum and Average in a Java Array](https://www.baeldung.com/java-array-sum-average)
|
||||
- [Arrays in Java: A Reference Guide](https://www.baeldung.com/java-arrays-guide)
|
||||
- [How to Reverse an Array in Java](http://www.baeldung.com/java-invert-array)
|
||||
- [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays)
|
||||
- [Checking If an Array Is Sorted in Java](https://www.baeldung.com/java-check-sorted-array)
|
||||
- [[More -->]](/core-java-modules/core-java-arrays-2)
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class ArrayInverter {
|
||||
|
||||
public void invertUsingFor(Object[] array) {
|
||||
for (int i = 0; i < array.length / 2; i++) {
|
||||
Object temp = array[i];
|
||||
array[i] = array[array.length - 1 - i];
|
||||
array[array.length - 1 - i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
public void invertUsingCollectionsReverse(Object[] array) {
|
||||
List<Object> list = Arrays.asList(array);
|
||||
Collections.reverse(list);
|
||||
}
|
||||
|
||||
public Object[] invertUsingStreams(final Object[] array) {
|
||||
return IntStream.rangeClosed(1, array.length)
|
||||
.mapToObj(i -> array[array.length - i])
|
||||
.toArray();
|
||||
}
|
||||
|
||||
public void invertUsingCommonsLang(Object[] array) {
|
||||
ArrayUtils.reverse(array);
|
||||
}
|
||||
|
||||
public Object[] invertUsingGuava(Object[] array) {
|
||||
List<Object> list = Arrays.asList(array);
|
||||
List<Object> reverted = Lists.reverse(list);
|
||||
return reverted.toArray();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class SortedArrayChecker {
|
||||
|
||||
boolean isSorted(int[] array, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
if (array[length - 2] > array[length - 1])
|
||||
return false;
|
||||
|
||||
return isSorted(array, length - 1);
|
||||
}
|
||||
|
||||
boolean isSorted(int[] array) {
|
||||
for (int i = 0; i < array.length - 1; i++) {
|
||||
if (array[i] > array[i + 1])
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSorted(Comparable[] array, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
if (array[length - 2].compareTo(array[length - 1]) > 0)
|
||||
return false;
|
||||
|
||||
return isSorted(array, length - 1);
|
||||
}
|
||||
|
||||
boolean isSorted(Comparable[] array) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (array[i].compareTo(array[i + 1]) > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSorted(Object[] array, Comparator comparator) {
|
||||
for (int i = 0; i < array.length - 1; ++i) {
|
||||
if (comparator.compare(array[i], (array[i + 1])) > 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean isSorted(Object[] array, Comparator comparator, int length) {
|
||||
if (array == null || length < 2)
|
||||
return true;
|
||||
|
||||
if (comparator.compare(array[length - 2], array[length - 1]) > 0)
|
||||
return false;
|
||||
|
||||
return isSorted(array, comparator, length - 1);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ArrayInverterUnitTest {
|
||||
|
||||
private String[] fruits = { "apples", "tomatoes", "bananas", "guavas", "pineapples", "oranges" };
|
||||
|
||||
@Test
|
||||
public void invertArrayWithForLoop() {
|
||||
ArrayInverter inverter = new ArrayInverter();
|
||||
inverter.invertUsingFor(fruits);
|
||||
|
||||
assertThat(new String[] { "oranges", "pineapples", "guavas", "bananas", "tomatoes", "apples" }).isEqualTo(fruits);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invertArrayWithCollectionsReverse() {
|
||||
ArrayInverter inverter = new ArrayInverter();
|
||||
inverter.invertUsingCollectionsReverse(fruits);
|
||||
|
||||
assertThat(new String[] { "oranges", "pineapples", "guavas", "bananas", "tomatoes", "apples" }).isEqualTo(fruits);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invertArrayWithStreams() {
|
||||
ArrayInverter inverter = new ArrayInverter();
|
||||
|
||||
assertThat(new String[] { "oranges", "pineapples", "guavas", "bananas", "tomatoes", "apples" }).isEqualTo(inverter.invertUsingStreams(fruits));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invertArrayWithCommonsLang() {
|
||||
ArrayInverter inverter = new ArrayInverter();
|
||||
inverter.invertUsingCommonsLang(fruits);
|
||||
|
||||
assertThat(new String[] { "oranges", "pineapples", "guavas", "bananas", "tomatoes", "apples" }).isEqualTo(fruits);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invertArrayWithGuava() {
|
||||
ArrayInverter inverter = new ArrayInverter();
|
||||
|
||||
assertThat(new String[] { "oranges", "pineapples", "guavas", "bananas", "tomatoes", "apples" }).isEqualTo(inverter.invertUsingGuava(fruits));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SortedArrayCheckerUnitTest {
|
||||
private static final int[] INTEGER_SORTED = {1, 3, 5, 7, 9};
|
||||
private static final int[] INTEGER_NOT_SORTED = {1, 3, 11, 7};
|
||||
|
||||
private static final String[] STRING_SORTED = {"abc", "cde", "fgh"};
|
||||
private static final String[] STRING_NOT_SORTED = {"abc", "fgh", "cde", "ijk"};
|
||||
|
||||
private static final Employee[] EMPLOYEES_SORTED_BY_NAME = {
|
||||
new Employee(1, "Carlos", 26),
|
||||
new Employee(2, "Daniel", 31),
|
||||
new Employee(3, "Marta", 27)};
|
||||
|
||||
private static final Employee[] EMPLOYEES_NOT_SORTED_BY_NAME = {
|
||||
new Employee(1, "Daniel", 31),
|
||||
new Employee(2, "Carlos", 26),
|
||||
new Employee(3, "Marta", 27)};
|
||||
|
||||
private static final Employee[] EMPLOYEES_SORTED_BY_AGE = {
|
||||
new Employee(1, "Carlos", 26),
|
||||
new Employee(2, "Marta", 27),
|
||||
new Employee(3, "Daniel", 31)};
|
||||
|
||||
private static final Employee[] EMPLOYEES_NOT_SORTED_BY_AGE = {
|
||||
new Employee(1, "Marta", 27),
|
||||
new Employee(2, "Carlos", 26),
|
||||
new Employee(3, "Daniel", 31)};
|
||||
|
||||
private SortedArrayChecker sortedArrayChecker;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
sortedArrayChecker = new SortedArrayChecker();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_thenReturnIfItIsSortedOrNot() {
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_SORTED)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_NOT_SORTED)).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_SORTED, INTEGER_SORTED.length)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(INTEGER_NOT_SORTED, INTEGER_NOT_SORTED.length)).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringArray_thenReturnIfItIsSortedOrNot() {
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_SORTED)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_NOT_SORTED)).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_SORTED, STRING_SORTED.length)).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(STRING_NOT_SORTED, STRING_NOT_SORTED.length)).isEqualTo(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_thenReturnIfItIsSortedOrNot() {
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_SORTED_BY_NAME, Comparator.comparing(Employee::getName))).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_NOT_SORTED_BY_NAME, Comparator.comparing(Employee::getName))).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge))).isEqualTo(true);
|
||||
assertThat(sortedArrayChecker.isSorted(EMPLOYEES_NOT_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge))).isEqualTo(false);
|
||||
|
||||
assertThat(sortedArrayChecker
|
||||
.isSorted(EMPLOYEES_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge), EMPLOYEES_SORTED_BY_AGE.length))
|
||||
.isEqualTo(true);
|
||||
assertThat(sortedArrayChecker
|
||||
.isSorted(EMPLOYEES_NOT_SORTED_BY_AGE, Comparator.comparingInt(Employee::getAge), EMPLOYEES_NOT_SORTED_BY_AGE.length))
|
||||
.isEqualTo(false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.baeldung.sort;
|
||||
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class ArraySortUnitTest {
|
||||
private Employee[] employees;
|
||||
private int[] numbers;
|
||||
private String[] strings;
|
||||
|
||||
private Employee john = new Employee(6, "John");
|
||||
private Employee mary = new Employee(3, "Mary");
|
||||
private Employee david = new Employee(4, "David");
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
createEmployeesArray();
|
||||
createNumbersArray();
|
||||
createStringArray();
|
||||
}
|
||||
|
||||
private void createEmployeesArray() {
|
||||
employees = new Employee[]{john, mary, david};
|
||||
}
|
||||
|
||||
private void createNumbersArray() {
|
||||
numbers = new int[]{-8, 7, 5, 9, 10, -2, 3};
|
||||
}
|
||||
|
||||
private void createStringArray() {
|
||||
strings = new String[]{"learning", "java", "with", "baeldung"};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenSortingAscending_thenCorrectlySorted() {
|
||||
Arrays.sort(numbers);
|
||||
|
||||
assertArrayEquals(new int[]{-8, -2, 3, 5, 7, 9, 10}, numbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntArray_whenSortingDescending_thenCorrectlySorted() {
|
||||
numbers = IntStream.of(numbers).boxed().sorted(Comparator.reverseOrder()).mapToInt(i -> i).toArray();
|
||||
|
||||
assertArrayEquals(new int[]{10, 9, 7, 5, 3, -2, -8}, numbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringArray_whenSortingAscending_thenCorrectlySorted() {
|
||||
Arrays.sort(strings);
|
||||
|
||||
assertArrayEquals(new String[]{"baeldung", "java", "learning", "with"}, strings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringArray_whenSortingDescending_thenCorrectlySorted() {
|
||||
Arrays.sort(strings, Comparator.reverseOrder());
|
||||
|
||||
assertArrayEquals(new String[]{"with", "learning", "java", "baeldung"}, strings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectArray_whenSortingAscending_thenCorrectlySorted() {
|
||||
Arrays.sort(employees, Comparator.comparing(Employee::getName));
|
||||
|
||||
assertArrayEquals(new Employee[]{david, john, mary}, employees);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectArray_whenSortingDescending_thenCorrectlySorted() {
|
||||
Arrays.sort(employees, Comparator.comparing(Employee::getName).reversed());
|
||||
|
||||
assertArrayEquals(new Employee[]{mary, john, david}, employees);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectArray_whenSortingMultipleAttributesAscending_thenCorrectlySorted() {
|
||||
Arrays.sort(employees, Comparator.comparing(Employee::getName).thenComparing(Employee::getId));
|
||||
|
||||
assertArrayEquals(new Employee[]{david, john, mary}, employees);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user