01-08 08:57
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_45 ์‚ฝ์ž… ์ •๋ ฌ ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_45 ์‚ฝ์ž… ์ •๋ ฌ

miinsun 2022. 1. 10. 22:32

 

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

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

 

 

 

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

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

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

6
11 7 5 6 10 9

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

5 6 7 9 10 11

 

 

โ€‹

๐Ÿ’ป Solution.java

import java.util.*;

public class Main {
	public void solution(int n, int [] arr) {
		int i, j = 0;
		for(i = 1; i < n; i++) {
			int tmp = arr[i];
			for(j = i - 1; j >= 0; j--) {
				if(arr[j] > tmp) {
					arr[j + 1] = arr[j];
				}
				else break;
			}
			arr[j + 1] = tmp;
		}
	
		for(int num : arr) {
			System.out.print(num + " ");
		}
		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