JAVA-12097: renamed algorithms-module to algorithms-modules
This commit is contained in:
4
algorithms-modules/algorithms-sorting/.gitignore
vendored
Normal file
4
algorithms-modules/algorithms-sorting/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/target/
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
17
algorithms-modules/algorithms-sorting/README.md
Normal file
17
algorithms-modules/algorithms-sorting/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
## Algorithms - Sorting
|
||||
|
||||
This module contains articles about sorting algorithms.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Bubble Sort in Java](https://www.baeldung.com/java-bubble-sort)
|
||||
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
|
||||
- [Quicksort Algorithm Implementation in Java](https://www.baeldung.com/java-quicksort)
|
||||
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
|
||||
- [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)
|
||||
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
|
||||
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
|
||||
- [Bucket Sort in Java](https://www.baeldung.com/java-bucket-sort)
|
||||
- More articles: [[next -->]](/algorithms-sorintg-2)
|
||||
40
algorithms-modules/algorithms-sorting/pom.xml
Normal file
40
algorithms-modules/algorithms-sorting/pom.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>algorithms-sorting</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>algorithms-sorting</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.algorithms.bubblesort;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BubbleSort {
|
||||
|
||||
void bubbleSort(Integer[] arr) {
|
||||
int n = arr.length;
|
||||
IntStream.range(0, n - 1)
|
||||
.flatMap(i -> IntStream.range(1, n - i))
|
||||
.forEach(j -> {
|
||||
if (arr[j - 1] > arr[j]) {
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[j - 1];
|
||||
arr[j - 1] = temp;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void optimizedBubbleSort(Integer[] arr) {
|
||||
int i = 0, n = arr.length;
|
||||
|
||||
boolean swapNeeded = true;
|
||||
while (i < n - 1 && swapNeeded) {
|
||||
swapNeeded = false;
|
||||
for (int j = 1; j < n - i; j++) {
|
||||
if (arr[j - 1] > arr[j]) {
|
||||
|
||||
int temp = arr[j - 1];
|
||||
arr[j - 1] = arr[j];
|
||||
arr[j] = temp;
|
||||
swapNeeded = true;
|
||||
}
|
||||
}
|
||||
if (!swapNeeded)
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.algorithms.bucketsort;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class IntegerBucketSorter implements Sorter<Integer> {
|
||||
|
||||
private final Comparator<Integer> comparator;
|
||||
|
||||
public IntegerBucketSorter(Comparator<Integer> comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
public IntegerBucketSorter() {
|
||||
comparator = Comparator.naturalOrder();
|
||||
}
|
||||
|
||||
public List<Integer> sort(List<Integer> arrayToSort) {
|
||||
|
||||
List<List<Integer>> buckets = splitIntoUnsortedBuckets(arrayToSort);
|
||||
|
||||
for(List<Integer> bucket : buckets){
|
||||
bucket.sort(comparator);
|
||||
}
|
||||
|
||||
return concatenateSortedBuckets(buckets);
|
||||
}
|
||||
|
||||
private List<Integer> concatenateSortedBuckets(List<List<Integer>> buckets){
|
||||
List<Integer> sortedArray = new LinkedList<>();
|
||||
for(List<Integer> bucket : buckets){
|
||||
sortedArray.addAll(bucket);
|
||||
}
|
||||
return sortedArray;
|
||||
}
|
||||
|
||||
private List<List<Integer>> splitIntoUnsortedBuckets(List<Integer> initialList){
|
||||
|
||||
final int max = findMax(initialList);
|
||||
final int numberOfBuckets = (int) Math.sqrt(initialList.size());
|
||||
|
||||
List<List<Integer>> buckets = new ArrayList<>();
|
||||
for(int i = 0; i < numberOfBuckets; i++) buckets.add(new ArrayList<>());
|
||||
|
||||
//distribute the data
|
||||
for (int i : initialList) {
|
||||
buckets.get(hash(i, max, numberOfBuckets)).add(i);
|
||||
}
|
||||
return buckets;
|
||||
|
||||
}
|
||||
|
||||
private int findMax(List<Integer> input){
|
||||
int m = Integer.MIN_VALUE;
|
||||
for (int i : input){
|
||||
m = Math.max(i, m);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
private static int hash(int i, int max, int numberOfBuckets) {
|
||||
return (int) ((double) i / max * (numberOfBuckets - 1));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.algorithms.bucketsort;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Sorter<T> {
|
||||
|
||||
List<T> sort(List<T> arrayToSort);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.algorithms.counting;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class CountingSort {
|
||||
|
||||
public static int[] sort(int[] input, int k) {
|
||||
verifyPreconditions(input, k);
|
||||
if (input.length == 0) return input;
|
||||
|
||||
int[] c = countElements(input, k);
|
||||
int[] sorted = new int[input.length];
|
||||
for (int i = input.length - 1; i >= 0; i--) {
|
||||
int current = input[i];
|
||||
sorted[c[current] - 1] = current;
|
||||
c[current] -= 1;
|
||||
}
|
||||
|
||||
return sorted;
|
||||
}
|
||||
|
||||
static int[] countElements(int[] input, int k) {
|
||||
int[] c = new int[k + 1];
|
||||
Arrays.fill(c, 0);
|
||||
for (int i : input) {
|
||||
c[i] += 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i < c.length; i++) {
|
||||
c[i] += c[i - 1];
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private static void verifyPreconditions(int[] input, int k) {
|
||||
if (input == null) {
|
||||
throw new IllegalArgumentException("Input is required");
|
||||
}
|
||||
|
||||
int min = IntStream.of(input).min().getAsInt();
|
||||
int max = IntStream.of(input).max().getAsInt();
|
||||
|
||||
if (min < 0 || max > k) {
|
||||
throw new IllegalArgumentException("The input numbers should be between zero and " + k);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.baeldung.algorithms.heapsort;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Heap<E extends Comparable<E>> {
|
||||
|
||||
private List<E> elements = new ArrayList<>();
|
||||
|
||||
public static <E extends Comparable<E>> List<E> sort(Iterable<E> elements) {
|
||||
Heap<E> heap = of(elements);
|
||||
|
||||
List<E> result = new ArrayList<>();
|
||||
|
||||
while (!heap.isEmpty()) {
|
||||
result.add(heap.pop());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <E extends Comparable<E>> Heap<E> of(E... elements) {
|
||||
return of(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
public static <E extends Comparable<E>> Heap<E> of(Iterable<E> elements) {
|
||||
Heap<E> result = new Heap<>();
|
||||
for (E element : elements) {
|
||||
result.add(element);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void add(E e) {
|
||||
elements.add(e);
|
||||
int elementIndex = elements.size() - 1;
|
||||
while (!isRoot(elementIndex) && !isCorrectChild(elementIndex)) {
|
||||
int parentIndex = parentIndex(elementIndex);
|
||||
swap(elementIndex, parentIndex);
|
||||
elementIndex = parentIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public E pop() {
|
||||
if (isEmpty()) {
|
||||
throw new IllegalStateException("You cannot pop from an empty heap");
|
||||
}
|
||||
|
||||
E result = elementAt(0);
|
||||
|
||||
int lasElementIndex = elements.size() - 1;
|
||||
swap(0, lasElementIndex);
|
||||
elements.remove(lasElementIndex);
|
||||
|
||||
int elementIndex = 0;
|
||||
while (!isLeaf(elementIndex) && !isCorrectParent(elementIndex)) {
|
||||
int smallerChildIndex = smallerChildIndex(elementIndex);
|
||||
swap(elementIndex, smallerChildIndex);
|
||||
elementIndex = smallerChildIndex;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return elements.isEmpty();
|
||||
}
|
||||
|
||||
private boolean isRoot(int index) {
|
||||
return index == 0;
|
||||
}
|
||||
|
||||
private int smallerChildIndex(int index) {
|
||||
int leftChildIndex = leftChildIndex(index);
|
||||
int rightChildIndex = rightChildIndex(index);
|
||||
|
||||
if (!isValidIndex(rightChildIndex)) {
|
||||
return leftChildIndex;
|
||||
}
|
||||
|
||||
if (elementAt(leftChildIndex).compareTo(elementAt(rightChildIndex)) < 0) {
|
||||
return leftChildIndex;
|
||||
}
|
||||
|
||||
return rightChildIndex;
|
||||
}
|
||||
|
||||
private boolean isLeaf(int index) {
|
||||
return !isValidIndex(leftChildIndex(index));
|
||||
}
|
||||
|
||||
private boolean isCorrectParent(int index) {
|
||||
return isCorrect(index, leftChildIndex(index)) && isCorrect(index, rightChildIndex(index));
|
||||
}
|
||||
|
||||
private boolean isCorrectChild(int index) {
|
||||
return isCorrect(parentIndex(index), index);
|
||||
}
|
||||
|
||||
private boolean isCorrect(int parentIndex, int childIndex) {
|
||||
if (!isValidIndex(parentIndex) || !isValidIndex(childIndex)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return elementAt(parentIndex).compareTo(elementAt(childIndex)) < 0;
|
||||
}
|
||||
|
||||
private boolean isValidIndex(int index) {
|
||||
return index < elements.size();
|
||||
}
|
||||
|
||||
private void swap(int index1, int index2) {
|
||||
E element1 = elementAt(index1);
|
||||
E element2 = elementAt(index2);
|
||||
elements.set(index1, element2);
|
||||
elements.set(index2, element1);
|
||||
}
|
||||
|
||||
private E elementAt(int index) {
|
||||
return elements.get(index);
|
||||
}
|
||||
|
||||
private int parentIndex(int index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
private int leftChildIndex(int index) {
|
||||
return 2 * index + 1;
|
||||
}
|
||||
|
||||
private int rightChildIndex(int index) {
|
||||
return 2 * index + 2;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
public class InsertionSort {
|
||||
|
||||
public static void insertionSortImperative(int[] input) {
|
||||
for (int i = 1; i < input.length; i++) {
|
||||
int key = input[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertionSortRecursive(int[] input) {
|
||||
insertionSortRecursive(input, input.length);
|
||||
}
|
||||
|
||||
private static void insertionSortRecursive(int[] input, int i) {
|
||||
// base case
|
||||
if (i <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// sort the first i - 1 elements of the array
|
||||
insertionSortRecursive(input, i - 1);
|
||||
|
||||
// then find the correct position of the element at position i
|
||||
int key = input[i - 1];
|
||||
int j = i - 2;
|
||||
// shifting the elements from their position by 1
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
// inserting the key at the appropriate position
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.algorithms.mergesort;
|
||||
|
||||
public class MergeSort {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] a = { 5, 1, 6, 2, 3, 4 };
|
||||
mergeSort(a, a.length);
|
||||
for (int i = 0; i < a.length; i++)
|
||||
System.out.println(a[i]);
|
||||
}
|
||||
|
||||
public static void mergeSort(int[] a, int n) {
|
||||
if (n < 2)
|
||||
return;
|
||||
int mid = n / 2;
|
||||
int[] l = new int[mid];
|
||||
int[] r = new int[n - mid];
|
||||
|
||||
for (int i = 0; i < mid; i++) {
|
||||
l[i] = a[i];
|
||||
}
|
||||
for (int i = mid; i < n; i++) {
|
||||
r[i - mid] = a[i];
|
||||
}
|
||||
mergeSort(l, mid);
|
||||
mergeSort(r, n - mid);
|
||||
|
||||
merge(a, l, r, mid, n - mid);
|
||||
}
|
||||
|
||||
public static void merge(int[] a, int[] l, int[] r, int left, int right) {
|
||||
|
||||
int i = 0, j = 0, k = 0;
|
||||
|
||||
while (i < left && j < right) {
|
||||
|
||||
if (l[i] <= r[j])
|
||||
a[k++] = l[i++];
|
||||
else
|
||||
a[k++] = r[j++];
|
||||
|
||||
}
|
||||
|
||||
while (i < left)
|
||||
a[k++] = l[i++];
|
||||
|
||||
while (j < right)
|
||||
a[k++] = r[j++];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
public class QuickSort {
|
||||
|
||||
public static void quickSort(int arr[], int begin, int end)
|
||||
{
|
||||
if (begin < end) {
|
||||
int partitionIndex = partition(arr, begin, end);
|
||||
|
||||
// Recursively sort elements of the 2 sub-arrays
|
||||
quickSort(arr, begin, partitionIndex-1);
|
||||
quickSort(arr, partitionIndex+1, end);
|
||||
}
|
||||
}
|
||||
|
||||
private static int partition(int arr[], int begin, int end)
|
||||
{
|
||||
int pivot = arr[end];
|
||||
int i = (begin-1);
|
||||
|
||||
for (int j=begin; j<end; j++)
|
||||
{
|
||||
if (arr[j] <= pivot) {
|
||||
i++;
|
||||
|
||||
int swapTemp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = swapTemp;
|
||||
}
|
||||
}
|
||||
|
||||
int swapTemp = arr[i+1];
|
||||
arr[i+1] = arr[end];
|
||||
arr[end] = swapTemp;
|
||||
|
||||
return i+1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
public class ThreeWayQuickSort {
|
||||
|
||||
public static void threeWayQuickSort(int[] a, int begin, int end)
|
||||
{
|
||||
if (end <= begin) return;
|
||||
|
||||
// partition
|
||||
int i = begin;
|
||||
int less = begin;
|
||||
int greater = end;
|
||||
|
||||
while (i <= greater){
|
||||
if (a[i] < a[less]) {
|
||||
int tmp = a[i];
|
||||
a[i] = a[less];
|
||||
a[less] = tmp;
|
||||
|
||||
i++;
|
||||
less++;
|
||||
}
|
||||
else if (a[less] < a[i]) {
|
||||
int tmp = a[i];
|
||||
a[i] = a[greater];
|
||||
a[greater] = tmp;
|
||||
|
||||
greater--;
|
||||
}
|
||||
else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
threeWayQuickSort(a, begin, less - 1);
|
||||
threeWayQuickSort(a, greater + 1, end);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.algorithms.radixsort;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class RadixSort {
|
||||
|
||||
public static void sort(int numbers[]) {
|
||||
int maximumNumber = findMaximumNumberIn(numbers);
|
||||
|
||||
int numberOfDigits = calculateNumberOfDigitsIn(maximumNumber);
|
||||
|
||||
int placeValue = 1;
|
||||
|
||||
while (numberOfDigits-- > 0) {
|
||||
applyCountingSortOn(numbers, placeValue);
|
||||
placeValue *= 10;
|
||||
}
|
||||
}
|
||||
|
||||
private static void applyCountingSortOn(int[] numbers, int placeValue) {
|
||||
int range = 10; // radix or the base
|
||||
|
||||
int length = numbers.length;
|
||||
int[] frequency = new int[range];
|
||||
int[] sortedValues = new int[length];
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int digit = (numbers[i] / placeValue) % range;
|
||||
frequency[digit]++;
|
||||
}
|
||||
|
||||
for (int i = 1; i < range; i++) {
|
||||
frequency[i] += frequency[i - 1];
|
||||
}
|
||||
|
||||
for (int i = length - 1; i >= 0; i--) {
|
||||
int digit = (numbers[i] / placeValue) % range;
|
||||
sortedValues[frequency[digit] - 1] = numbers[i];
|
||||
frequency[digit]--;
|
||||
}
|
||||
|
||||
System.arraycopy(sortedValues, 0, numbers, 0, length);
|
||||
}
|
||||
|
||||
private static int calculateNumberOfDigitsIn(int number) {
|
||||
return (int) Math.log10(number) + 1; // valid only if number > 0
|
||||
}
|
||||
|
||||
private static int findMaximumNumberIn(int[] arr) {
|
||||
return Arrays.stream(arr).max().getAsInt();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.algorithms.selectionsort;
|
||||
|
||||
public class SelectionSort {
|
||||
|
||||
public static void sortAscending(final int[] arr) {
|
||||
for (int i = 0; i < arr.length - 1; i++) {
|
||||
int minElementIndex = i;
|
||||
for (int j = i + 1; j < arr.length; j++) {
|
||||
if (arr[minElementIndex] > arr[j]) {
|
||||
minElementIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (minElementIndex != i) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minElementIndex];
|
||||
arr[minElementIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sortDescending(final int[] arr) {
|
||||
for (int i = 0; i < arr.length - 1; i++) {
|
||||
int maxElementIndex = i;
|
||||
for (int j = i + 1; j < arr.length; j++) {
|
||||
if (arr[maxElementIndex] < arr[j]) {
|
||||
maxElementIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxElementIndex != i) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[maxElementIndex];
|
||||
arr[maxElementIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.shellsort;
|
||||
|
||||
public class ShellSort {
|
||||
|
||||
public static void sort(int arrayToSort[]) {
|
||||
int n = arrayToSort.length;
|
||||
|
||||
for (int gap = n / 2; gap > 0; gap /= 2) {
|
||||
for (int i = gap; i < n; i++) {
|
||||
int key = arrayToSort[i];
|
||||
int j = i;
|
||||
while (j >= gap && arrayToSort[j - gap] > key) {
|
||||
arrayToSort[j] = arrayToSort[j - gap];
|
||||
j -= gap;
|
||||
}
|
||||
arrayToSort[j] = key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.bubblesort;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BubbleSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithBubbleSort_thenGetSortedArray() {
|
||||
Integer[] array = { 2, 1, 4, 6, 3, 5 };
|
||||
Integer[] sortedArray = { 1, 2, 3, 4, 5, 6 };
|
||||
BubbleSort bubbleSort = new BubbleSort();
|
||||
bubbleSort.bubbleSort(array);
|
||||
assertArrayEquals(array, sortedArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithOptimizedBubbleSort_thenGetSortedArray() {
|
||||
Integer[] array = { 2, 1, 4, 6, 3, 5 };
|
||||
Integer[] sortedArray = { 1, 2, 3, 4, 5, 6 };
|
||||
BubbleSort bubbleSort = new BubbleSort();
|
||||
bubbleSort.optimizedBubbleSort(array);
|
||||
assertArrayEquals(array, sortedArray);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.algorithms.bucketsort;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IntegerBucketSorterUnitTest {
|
||||
|
||||
private IntegerBucketSorter sorter;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
sorter = new IntegerBucketSorter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedList_whenSortedUsingBucketSorter_checkSortingCorrect() {
|
||||
|
||||
List<Integer> unsorted = Arrays.asList(80,50,60,30,20,10,70,0,40,500,600,602,200,15);
|
||||
List<Integer> expected = Arrays.asList(0,10,15,20,30,40,50,60,70,80,200,500,600,602);
|
||||
|
||||
List<Integer> actual = sorter.sort(unsorted);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.algorithms.counting;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CountingSortUnitTest {
|
||||
|
||||
@Test
|
||||
void countElements_GivenAnArray_ShouldCalculateTheFrequencyArrayAsExpected() {
|
||||
int k = 5;
|
||||
int[] input = { 4, 3, 2, 5, 4, 3, 5, 1, 0, 2, 5 };
|
||||
|
||||
int[] c = CountingSort.countElements(input, k);
|
||||
int[] expected = { 1, 2, 4, 6, 8, 11 };
|
||||
assertArrayEquals(expected, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sort_GivenAnArray_ShouldSortTheInputAsExpected() {
|
||||
int k = 5;
|
||||
int[] input = { 4, 3, 2, 5, 4, 3, 5, 1, 0, 2, 5 };
|
||||
|
||||
int[] sorted = CountingSort.sort(input, k);
|
||||
|
||||
// Our sorting algorithm and Java's should return the same result
|
||||
Arrays.sort(input);
|
||||
assertArrayEquals(input, sorted);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.algorithms.heapsort;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class HeapUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenNotEmptyHeap_whenPopCalled_thenItShouldReturnSmallestElement() {
|
||||
// given
|
||||
Heap<Integer> heap = Heap.of(3, 5, 1, 4, 2);
|
||||
|
||||
// when
|
||||
int head = heap.pop();
|
||||
|
||||
// then
|
||||
assertThat(head).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNotEmptyIterable_whenSortCalled_thenItShouldReturnElementsInSortedList() {
|
||||
// given
|
||||
List<Integer> elements = Arrays.asList(3, 5, 1, 4, 2);
|
||||
|
||||
// when
|
||||
List<Integer> sortedElements = Heap.sort(elements);
|
||||
|
||||
// then
|
||||
assertThat(sortedElements).isEqualTo(Arrays.asList(1, 2, 3, 4, 5));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
import com.baeldung.algorithms.insertionsort.InsertionSort;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class InsertionSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortImperative_thenSortedAsc() {
|
||||
int[] input = {6, 2, 3, 4, 5, 1};
|
||||
InsertionSort.insertionSortImperative(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortRecursive_thenSortedAsc() {
|
||||
int[] input = {6, 4, 5, 2, 3, 1};
|
||||
InsertionSort.insertionSortRecursive(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.mergesort;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MergeSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void positiveTest() {
|
||||
int[] actual = { 5, 1, 6, 2, 3, 4 };
|
||||
int[] expected = { 1, 2, 3, 4, 5, 6 };
|
||||
MergeSort.mergeSort(actual, actual.length);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import com.baeldung.algorithms.quicksort.QuickSort;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class QuickSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithQuickSort_thenGetSortedArray() {
|
||||
int[] actual = { 9, 5, 1, 0, 6, 2, 3, 4, 7, 8 };
|
||||
int[] expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
QuickSort.quickSort(actual, 0, actual.length-1);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ThreeWayQuickSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithThreeWayQuickSort_thenGetSortedArray() {
|
||||
int[] actual = { 3, 5, 5, 5, 3, 7, 7, 3, 5, 5, 7, 3, 3 };
|
||||
int[] expected = { 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7 };
|
||||
ThreeWayQuickSort.threeWayQuickSort(actual, 0, actual.length-1);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.algorithms.radixsort;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RadixSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenRadixSort_thenArraySorted() {
|
||||
int[] numbers = { 387, 468, 134, 123, 68, 221, 769, 37, 7 };
|
||||
RadixSort.sort(numbers);
|
||||
int[] numbersSorted = { 7, 37, 68, 123, 134, 221, 387, 468, 769 };
|
||||
assertArrayEquals(numbersSorted, numbers);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.algorithms.selectionsort;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SelectionSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenSelectionSort_SortAscending_thenSortedAsc() {
|
||||
int[] input = { 5, 4, 1, 6, 2 };
|
||||
SelectionSort.sortAscending(input);
|
||||
int[] expected = {1, 2, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenSelectionSort_SortDescending_thenSortedDesc() {
|
||||
int[] input = { 5, 4, 1, 6, 2 };
|
||||
SelectionSort.sortDescending(input);
|
||||
int[] expected = {6, 5, 4, 2, 1};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.shellsort;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ShellSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenShellSort_thenSortedAsc() {
|
||||
int[] input = {41, 15, 82, 5, 65, 19, 32, 43, 8};
|
||||
ShellSort.sort(input);
|
||||
int[] expected = {5, 8, 15, 19, 32, 41, 43, 65, 82};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user