SelectSort 선택 정렬

2540 단어 select
//SelectSort (( O(n²)))

public class TestSelectSort {

    

    public int[] selectSortArray(int[] arr){

        int min_index;

        

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

            min_index = i;

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

                if(arr[j] < min_index){

                    min_index = j;

                }

                if(min_index != i){

                    int temp;

                    temp = arr[i];

                    arr[i] = arr[min_index];

                    arr[min_index] = temp;

                }

            }

        }

        

        return arr;

    }

    

    public static void main(String[] args) {

        int[] arr = {6,2,4,1,5,9};

        TestSelectSort test = new TestSelectSort();

        test.selectSortArray(arr);

        

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

            System.out.println(arr[i]);

        }

    }



}

정렬 선택 - 매번 가장 작거나 큰 배열이 상응하는 위치에 있다

좋은 웹페이지 즐겨찾기