Skip to content

Commit

Permalink
[정렬] 선택 정렬
Browse files Browse the repository at this point in the history
  • Loading branch information
ones1kk committed Nov 9, 2023
1 parent b06f458 commit a00886c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/io/github/ones1kk/study/sorting/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.ones1kk.study.sorting;

public class SelectionSort {

public static void selectionSort(int[] arr, int size) {
for (int i = 0; i < size - 1; i++) {
int min_index = i;

for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}

swap(arr, min_index, i);
}
}

private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.ones1kk.study.sorting;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;

class SelectionSortTest {

@ParameterizedTest
@ValueSource(strings = {
"3,7,5,2,1"
, "102,1,4,4214,123,3,6"
})
@DisplayName("선택 정렬")
void testSelectionSort(String value) {
// given
int[] arr = Arrays.stream(value.split(","))
.mapToInt(Integer::parseInt)
.toArray();

// when
SelectionSort.selectionSort(arr, arr.length);

// then
assertThat(arr).isSorted();
}

}

0 comments on commit a00886c

Please sign in to comment.