Created
October 2, 2016 19:37
-
-
Save thieux/aec720c9c340f6013e1592f84bdafada to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mathieupauly.selectionsort; | |
| import org.junit.Test; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| public class SelectionSortTest { | |
| private SelectionSort selectionSort = new SelectionSort(); | |
| @Test | |
| public void empty() { | |
| final int[] array = {}; | |
| selectionSort.sort(array); | |
| assertThat(array).isEqualTo(new int[]{}); | |
| } | |
| @Test | |
| public void two_element_sorted() { | |
| final int[] array = {0, 1}; | |
| selectionSort.sort(array); | |
| assertThat(array).isEqualTo(new int[]{0, 1}); | |
| } | |
| @Test | |
| public void test() { | |
| final int[] array = {1, 0}; | |
| selectionSort.sort(array); | |
| assertThat(array).isEqualTo(new int[]{0, 1}); | |
| } | |
| } | |
| class SelectionSort { | |
| public void sort(int[] array) { | |
| int minIndex; | |
| for (int i = 0; i < array.length - 1; i++) { | |
| minIndex = i; | |
| for (int j = i + 1; j < array.length; j++) { | |
| if (array[j] < array[minIndex]) { | |
| minIndex = j; | |
| } | |
| } | |
| if (minIndex != i) { | |
| int tmp = array[i]; | |
| array[i] = array[minIndex]; | |
| array[minIndex] = tmp; | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment