Add insertion sort algorithm

This commit is contained in:
William Brawner 2018-10-05 23:43:33 -05:00
parent db62e6140b
commit 95392eb38e
4 changed files with 96 additions and 5 deletions

View file

@ -24,5 +24,29 @@
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings>
</DBN-SQL>
<DBN-PSQL>
<case-options enabled="false">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false" />
</DBN-PSQL>
<DBN-SQL>
<case-options enabled="false">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false">
<option name="STATEMENT_SPACING" value="one_line" />
<option name="CLAUSE_CHOP_DOWN" value="chop_down_if_statement_long" />
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings>
</DBN-SQL>
</code_scheme>
</component>

View file

@ -3,12 +3,15 @@
This repository is meant for me to compile a list of examples of different
algorithms that I have learned of, and develop a working implementation.
These implementations may not be the most efficient, so please do point it
out to me if you see something that could be improved upon.
out to me if you see something that could be improved upon. The complexity
notations were taken from the [Big O Cheatsheet](http://bigocheatsheet.com/)
## Sorting
### Sorting
- [Merge Sort](sorting/src/main/java/com/wbrawner/algorithms/sorting/MergeSort.java)
- [Tests](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedMergeSortTest.java)
Algorithm|Implementation|Tests|Worst Time Complexity|Worst Space Complexity
-----|-----|-----|-----|-----
[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)
## Building/Testing
@ -16,4 +19,4 @@ out to me if you see something that could be improved upon.
To keep things simple, the tests share the same data where possible.
- [Sorting test data](sorting/src/test/java/com/wbrawner/algorithms/sorting/SortData.java)
- [Sorting algorithms test data](sorting/src/test/java/com/wbrawner/algorithms/sorting/SortData.java)

View file

@ -0,0 +1,33 @@
package com.wbrawner.algorithms.sorting;
/**
* The Insertion Sort algorithm modifies an array in place, removing the
* first untouched index and iterating over the already sorted indices
* to position the removed index correctly.
*/
public class InsertionSort {
/**
* Sort a given array of ints using the Insertion 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;
}
for (int i = 1; i < array.length; i++) {
int currentValue = array[i];
int j = i - 1;
while (j >= 0 && array[j] > currentValue) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = currentValue;
}
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 ParameterizedInsertionSortTest {
@Parameterized.Parameter(1)
public int[] sorted;
@Parameterized.Parameter()
public int[] unsorted;
@Test
public void sortTest() {
assertArrayEquals(
sorted,
InsertionSort.sort(unsorted)
);
}
@Parameterized.Parameters
public static int[][][] getData() {
return SortData.get();
}
}