Add selection sort algorithm
This commit is contained in:
parent
16ccd7a642
commit
036d2f400c
3 changed files with 67 additions and 0 deletions
|
@ -13,6 +13,7 @@ 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)
|
||||
[Selection Sort](https://en.wikipedia.org/wiki/Selection_sort)|[SelectionSort.java](sorting/src/main/java/com/wbrawner/algorithms/sorting/SelectionSort.java)|[ParameterizedSelectionSortTest.java](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedSelectionSortTest.java)|O(n²)|O(1)
|
||||
|
||||
## Building/Testing
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.wbrawner.algorithms.sorting;
|
||||
|
||||
/**
|
||||
* The Selection Sort algorithm modifies an array in place, finding the
|
||||
* minimum value that hasn't been sorted, and swapping it with the first
|
||||
* unsorted value.
|
||||
*/
|
||||
public class SelectionSort {
|
||||
/**
|
||||
* Sort a given array of ints using the Selection 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 = 0; i < array.length; i++) {
|
||||
int minPos = i;
|
||||
for (int j = i; j < array.length; j++) {
|
||||
if (array[j] < array[minPos]) {
|
||||
minPos = j;
|
||||
}
|
||||
}
|
||||
int min = array[minPos];
|
||||
array[minPos] = array[i];
|
||||
array[i] = min;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
|
@ -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 ParameterizedSelectionSortTest {
|
||||
|
||||
|
||||
@Parameterized.Parameter(1)
|
||||
public int[] sorted;
|
||||
|
||||
@Parameterized.Parameter()
|
||||
public int[] unsorted;
|
||||
|
||||
@Test
|
||||
public void sortTest() {
|
||||
assertArrayEquals(
|
||||
sorted,
|
||||
SelectionSort.sort(unsorted)
|
||||
);
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static int[][][] getData() {
|
||||
return SortData.get();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue