백준 단계별

[Java] 백준 2750: 수 정렬하기

pullwall 2023. 2. 2. 14:17
728x90
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] arr = new int[N];

        for(int i=0 ; i<N ; i++){
            arr[i] = sc.nextInt();
        }

        for(int i=0;i<N;i++){
            int minindex=i;
            for(int j=i+1;j<N;j++){
                if(arr[minindex]>arr[j]){
                    minindex=j;
                }
            }

            int tmp=arr[i];
            arr[i]=arr[minindex];
            arr[minindex]=tmp;
        }
        for(int val : arr){
            System.out.println(val);
        }
    }
}

https://welldonecode.tistory.com/73

 

[Java] 정렬 알고리즘 - 선택정렬(Selection Sort)

import java.util.Arrays; public class Main { public static void main(String[] args){ int[] arr = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8}; for(int i=0; i

welldonecode.tistory.com

 

 

 

가장 기본적인 선택정렬 알고리즘을 이용하여 문제를 해결하였다.

 

시간복잡도가 마찬가지로 O(n^2)인 선택정렬, 버블정렬을 이용해서도 해결할 수 있을 것이다.

 

 

728x90