diff --git a/README.md b/README.md index d3a7952..87ab68f 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ notations were taken from the [Big O Cheatsheet](http://bigocheatsheet.com/) Algorithm|Implementation|Tests|Worst Time Complexity|Worst Space Complexity -----|-----|-----|-----|----- +[Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort)|[BubbleSort.java](sorting/src/main/java/com/wbrawner/algorithms/sorting/BubbleSort.java)|[ParameterizedBubbleSortTest.java](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedBubbleSortTest.java)|O(n²)|O(1) [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort)|[InsertionSort.java](sorting/src/main/java/com/wbrawner/algorithms/sorting/InsertionSort.java)|[ParameterizedInsertionSortTest.java](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedInsertionSortTest.java)|O(n²)|O(1) [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort)|[MergeSort.java](sorting/src/main/java/com/wbrawner/algorithms/sorting/MergeSort.java)|[ParameterizedMergeSortTest.java](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedMergeSortTest.java)|O(n log(n))|O(n) diff --git a/sorting/src/main/java/com/wbrawner/algorithms/sorting/BubbleSort.java b/sorting/src/main/java/com/wbrawner/algorithms/sorting/BubbleSort.java new file mode 100644 index 0000000..1fe8b1e --- /dev/null +++ b/sorting/src/main/java/com/wbrawner/algorithms/sorting/BubbleSort.java @@ -0,0 +1,36 @@ +package com.wbrawner.algorithms.sorting; + +/** + * The Bubble Sort algorithm modifies an array in place, continuously + * looping through the array and swapping elements that are unsorted until no + * elements have been swapped. + */ +public class BubbleSort { + /** + * Sort a given array of ints using the Bubble Sort algorithm + * + * @param array an unsorted array of ints + * @return a sorted array of ints + */ + public static int[] sort(int[] array) { + // An empty array or array with only a single value can't be sorted + if (array.length < 2) { + return array; + } + + boolean swapsMade; + do { + swapsMade = false; + for (int i = 1; i < array.length; i++) { + if (array[i] < array[i - 1]) { + int currentValue = array[i]; + array[i] = array[i - 1]; + array[i - 1] = currentValue; + swapsMade = true; + } + } + } while (swapsMade); + + return array; + } +} diff --git a/sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedBubbleSortTest.java b/sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedBubbleSortTest.java new file mode 100644 index 0000000..34b8c9d --- /dev/null +++ b/sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedBubbleSortTest.java @@ -0,0 +1,31 @@ +package com.wbrawner.algorithms.sorting; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.junit.Assert.assertArrayEquals; + +@RunWith(Parameterized.class) +public class ParameterizedBubbleSortTest { + + + @Parameterized.Parameter(1) + public int[] sorted; + + @Parameterized.Parameter() + public int[] unsorted; + + @Test + public void sortTest() { + assertArrayEquals( + sorted, + BubbleSort.sort(unsorted) + ); + } + + @Parameterized.Parameters + public static int[][][] getData() { + return SortData.get(); + } +} \ No newline at end of file diff --git a/sorting/src/test/java/com/wbrawner/algorithms/sorting/SortData.java b/sorting/src/test/java/com/wbrawner/algorithms/sorting/SortData.java index c3518e0..34ea6fa 100644 --- a/sorting/src/test/java/com/wbrawner/algorithms/sorting/SortData.java +++ b/sorting/src/test/java/com/wbrawner/algorithms/sorting/SortData.java @@ -51,6 +51,20 @@ public class SortData { // Sorted values new int[]{1} }, + // Seventh data set - a reverse sorted array + new int[][]{ + // Unsorted values + new int[]{100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0}, + // Sorted values + new int[]{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100} + }, + // Eight data set - an already sorted array + new int[][]{ + // Unsorted values + new int[]{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, + // Sorted values + new int[]{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100} + }, }; } }