Posted at

Javaでセレクションソート

More than 3 years have passed since last update.


SelectionSort.java

import java.util.Arrays;

public class SelectionSort {
public static void main(String[] args) {
int[] arr = {5, 3, 2, 4, 7, 8, 1, 9, 0, 6};
Arrays.stream(selectionSort(arr)).forEach(e -> System.out.println(e));
}
public static int[] selectionSort(int[] arr) {
for(int i=0; i<arr.length-1; i++) {
int min = i;
for(int j=i+1; j<arr.length; j++) {
if(arr[min] > arr[j]) min = j;
}
if(min != i) {
int tmp = arr[min];
arr[min] = arr[i];
arr[i] = tmp;
}
}
return arr;
}
}