I've become a bit rusty in Java. This is an attempt at brushing up my skills.
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list.
Code
import java.util.Comparator;
import java.util.Objects;
public class Sorting {
/**
* Day 07 - Bubble Sort
* In place sort given array with bubble sort algorithm
*
* @param data array
* @param comparator implementation of comparator for given data type
* @param ascending sort order
* @param <T> data type of array elements
* @return sorted data array
*/
public static <T> T[] bubbleSort(T[] data, Comparator<T> comparator, boolean ascending) {
Objects.requireNonNull(data);
int len = data.length;
if (len <= 0) return data;
T temp;
boolean swapped;
for (int i = 0; i < len - 1; i++) {
swapped = false;
for (int j = 0; j < len - 1 - i; j++) {
if ((comparator.compare(data[j], data[j + 1]) * (ascending ? 1 : -1)) > 0) {
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
swapped = true;
}
}
if (!swapped) break;
}
return data;
}
}
Test cases
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestSorting {
@Test
public void testBubbleSort() {
Assert.assertEquals(asc(1, 2, 3), ar(1, 2, 3));
Assert.assertEquals(asc(3, 2, 1), ar(1, 2, 3));
Assert.assertEquals(asc(2, 0, -1, 2, 1), ar(-1, 0, 1, 2, 2));
Assert.assertEquals(dsc(1, 2, 3), ar(3, 2, 1));
Assert.assertEquals(dsc(3, 2, 1), ar(3, 2, 1));
Assert.assertEquals(dsc(2, 0, -1, 2, 1), ar(2, 2, 1, 0, -1));
Assert.assertEquals(asc(2, 1), ar(1, 2));
Assert.assertEquals(asc(1), ar(1));
Assert.assertEquals(asc(), ar());
Assert.assertEquals(dsc(1), ar(1));
Assert.assertEquals(dsc(), ar());
}
@Test(expectedExceptions = {NullPointerException.class})
public void testNullExcept() {
Sorting.bubbleSort(null, Integer::compare, true);
}
private static Integer[] ar(Integer... objects) {
return objects;
}
private static Integer[] asc(Integer... objects) {
return Sorting.bubbleSort(objects, Integer::compare, true);
}
private static Integer[] dsc(Integer... objects) {
return Sorting.bubbleSort(objects, Integer::compare, false);
}
}