01-09 04:37
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_44 ๋ฒ„๋ธ” ์ •๋ ฌ ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_44 ๋ฒ„๋ธ” ์ •๋ ฌ

miinsun 2022. 1. 10. 22:20

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

N๊ฐœ์ด ์ˆซ์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฒ„๋ธ” ์ •๋ ฌ์ž…๋‹ˆ๋‹ค.

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ - ์ฒซ ๋ฒˆ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1<=N<=100)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ์ž์—ฐ์ˆ˜๋Š” ์ •์ˆ˜ํ˜• ๋ฒ”์œ„ ์•ˆ์— ์žˆ์Šต๋‹ˆ๋‹ค.

6
13 5 11 7 23 15

์ถœ๋ ฅ - ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

5 7 11 13 15 23

 

 

โ€‹

๐Ÿ’ป Solution.java


import java.util.*;

public class Main {
	public void solution(int n, int [] arr) {
		for(int i = 0; i < n; i++) {
			for(int j = 1; j < n - i; j++) {
				if(arr[j - 1] > arr[j]) {
					int tmp = arr[j-1];
					arr[j-1] = arr[j];
					arr[j] = tmp;
				}
			}
		}
		for(int i : arr) {
			System.out.print(i + " ");
		}
		return;
	}
	
	public static void main(String[] args){
		Main main  = new Main();
		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();
		}
		main.solution(n, arr);
		sc.close();
		return ;
	}
}

 

 

 

Comments