Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

잉여의 IT

[백준 2750번: 수 정렬하기] 자바 문제풀이(선택정렬) 본문

백준 풀이

[백준 2750번: 수 정렬하기] 자바 문제풀이(선택정렬)

프로잉여 2022. 7. 22. 19:58

백준 정렬 문제 중 가장 기초적인 문제이다. 문제 설명란을 보면,

"시간 복잡도가 O(n²)인 정렬 알고리즘으로 풀 수 있습니다. 예를 들면 삽입 정렬, 거품 정렬 등이 있습니다."라고 한다.

고민도 안하고 선택정렬을 이용했다.

import java.util.Scanner;
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++) {
			for(int j=i+1; j<N; j++) {
				if(arr[i]>arr[j]) {
					int tmp = arr[i];
					arr[i] = arr[j];
					arr[j]=tmp;
				}
					
			}
		}
		for(int i=0; i<N; i++)
			System.out.println(arr[i]);
		
		sc.close();
	}

}

 

 

Comments