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
'백준 단계별' 카테고리의 다른 글
[Java] 백준 25305: 커트라인 (0) | 2023.02.02 |
---|---|
[Java] 백준 2587: 대표값2 (0) | 2023.02.02 |
[Java] 백준 2563: 색종이 (0) | 2023.01.26 |
[Java] 백준 2566: 최댓값 (0) | 2023.01.26 |
[Java] 백준 2738: 항렬 덧셈 (0) | 2023.01.25 |