LoginSignup
2
2

More than 5 years have passed since last update.

Javaでセレクションソート

Posted at
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;
    }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2