//

Selection Sort

selection sort

package sorting;

import java.util.Arrays;

public class SelectionSort {
    public static void main(String[] args) {
        int[] arr = new int[]{5, 4, 3, 2, 1};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    static void selectionSort(int[] arr) {

        for (int i = 0; i < arr.length; i++) {

            int el = i;
            for(int j = i + 1; j < arr.length; j++){
                if(arr[j] < arr[el]){
                    el = j;
                }
            }

            int temp = arr[el];
            arr[el] = arr[i];
            arr[i] = temp;
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *