Add bubble sort algorithm

This commit is contained in:
William Brawner 2018-10-06 00:03:28 -05:00
parent 95392eb38e
commit 16ccd7a642
4 changed files with 82 additions and 0 deletions

View file

@ -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)

View file

@ -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;
}
}

View file

@ -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();
}
}

View file

@ -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}
},
};
}
}