JAVA-615 Split or move algorithms-sorting module
This commit is contained in:
@@ -11,10 +11,7 @@ This module contains articles about sorting algorithms.
|
||||
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort)
|
||||
- [Shell Sort in Java](https://www.baeldung.com/java-shell-sort)
|
||||
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)
|
||||
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
|
||||
- [How an In-Place Sorting Algorithm Works](https://www.baeldung.com/java-in-place-sorting)
|
||||
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
|
||||
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
|
||||
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
|
||||
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
|
||||
- [Bucket Sort in Java](https://www.baeldung.com/java-bucket-sort)
|
||||
- More articles: [[next -->]](/algorithms-sorintg-2)
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.algorithms.sort.bynumber;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public final class NaturalOrderComparators {
|
||||
|
||||
private static final String DIGIT_AND_DECIMAL_REGEX = "[^\\d.]";
|
||||
|
||||
private NaturalOrderComparators() {
|
||||
throw new AssertionError("Let's keep this static");
|
||||
}
|
||||
|
||||
public static Comparator<String> createNaturalOrderRegexComparator() {
|
||||
return Comparator.comparingDouble(NaturalOrderComparators::parseStringToNumber);
|
||||
}
|
||||
|
||||
private static double parseStringToNumber(String input){
|
||||
|
||||
final String digitsOnly = input.replaceAll(DIGIT_AND_DECIMAL_REGEX, "");
|
||||
|
||||
if("".equals(digitsOnly)) return 0;
|
||||
|
||||
try{
|
||||
return Double.parseDouble(digitsOnly);
|
||||
}catch (NumberFormatException nfe){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.algorithms.stringsort;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class AnagramValidator {
|
||||
|
||||
public static boolean isValid(String text, String anagram) {
|
||||
text = prepare(text);
|
||||
anagram = prepare(anagram);
|
||||
|
||||
String sortedText = sort(text);
|
||||
String sortedAnagram = sort(anagram);
|
||||
|
||||
return sortedText.equals(sortedAnagram);
|
||||
}
|
||||
|
||||
private static String sort(String text) {
|
||||
char[] chars = prepare(text).toCharArray();
|
||||
|
||||
Arrays.sort(chars);
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
private static String prepare(String text) {
|
||||
return text.toLowerCase()
|
||||
.trim()
|
||||
.replaceAll("\\s+", "");
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package com.baeldung.algorithms.sort.bynumber;
|
||||
|
||||
import com.baeldung.algorithms.sort.bynumber.NaturalOrderComparators;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class NaturalOrderComparatorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSimpleStringsContainingIntsAndDoubles_whenSortedByRegex_checkSortingCorrect() {
|
||||
|
||||
List<String> testStrings = Arrays.asList("a1", "b3", "c4", "d2.2", "d2.4", "d2.3d");
|
||||
|
||||
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||
|
||||
List<String> expected = Arrays.asList("a1", "d2.2", "d2.3d", "d2.4", "b3", "c4");
|
||||
|
||||
assertEquals(expected, testStrings);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSimpleStringsContainingIntsAndDoublesWithAnInvalidNumber_whenSortedByRegex_checkSortingCorrect() {
|
||||
|
||||
List<String> testStrings = Arrays.asList("a1", "b3", "c4", "d2.2", "d2.4", "d2.3.3d");
|
||||
|
||||
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||
|
||||
List<String> expected = Arrays.asList("d2.3.3d", "a1", "d2.2", "d2.4", "b3", "c4");
|
||||
|
||||
assertEquals(expected, testStrings);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllForseenProblems_whenSortedByRegex_checkSortingCorrect() {
|
||||
|
||||
List<String> testStrings = Arrays.asList("a1", "b3", "c4", "d2.2", "d2.f4", "d2.3.3d");
|
||||
|
||||
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||
|
||||
List<String> expected = Arrays.asList("d2.3.3d", "a1", "d2.2", "d2.f4", "b3", "c4");
|
||||
|
||||
assertEquals(expected, testStrings);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComplexStringsContainingSeparatedNumbers_whenSortedByRegex_checkNumbersCondensedAndSorted() {
|
||||
|
||||
List<String> testStrings = Arrays.asList("a1b2c5", "b3ght3.2", "something65.thensomething5"); //125, 33.2, 65.5
|
||||
|
||||
List<String> expected = Arrays.asList("b3ght3.2", "something65.thensomething5", "a1b2c5" );
|
||||
|
||||
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||
|
||||
assertEquals(expected, testStrings);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringsNotContainingNumbers_whenSortedByRegex_checkOrderNotChanged() {
|
||||
|
||||
List<String> testStrings = Arrays.asList("a", "c", "d", "e");
|
||||
List<String> expected = new ArrayList<>(testStrings);
|
||||
|
||||
testStrings.sort(NaturalOrderComparators.createNaturalOrderRegexComparator());
|
||||
|
||||
assertEquals(expected, testStrings);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.algorithms.stringsort;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AnagramValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
void givenValidAnagrams_whenSorted_thenEqual() {
|
||||
boolean isValidAnagram = AnagramValidator.isValid("Avida Dollars", "Salvador Dali");
|
||||
|
||||
assertTrue(isValidAnagram);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNotValidAnagrams_whenSorted_thenNotEqual() {
|
||||
boolean isValidAnagram = AnagramValidator.isValid("abc", "def");
|
||||
|
||||
assertFalse(isValidAnagram);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.algorithms.stringsort;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class SortStringUnitTest {
|
||||
|
||||
@Test
|
||||
void givenString_whenSort_thenSorted() {
|
||||
String abcd = "bdca";
|
||||
char[] chars = abcd.toCharArray();
|
||||
|
||||
Arrays.sort(chars);
|
||||
String sorted = new String(chars);
|
||||
|
||||
assertThat(sorted).isEqualTo("abcd");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenSortJava8_thenSorted() {
|
||||
String sorted = "bdca".chars()
|
||||
.sorted()
|
||||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
|
||||
.toString();
|
||||
|
||||
assertThat(sorted).isEqualTo("abcd");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user