Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags more
Archives
Today
Total
관리 메뉴

공부블로그

[백준] 2750 본문

IT/알고리즘

[백준] 2750

So1_b 2022. 9. 18. 13:57

https://www.acmicpc.net/submit/2750/49300262

 

로그인

 

www.acmicpc.net

[문제 설명]

- 셸 알고리즘 사용

 

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int length = scan.nextInt();
		
		int[] arr = new int[length];
		for(int i=0; i<length; i++) {
			arr[i] = scan.nextInt();
		}
		sort(arr, length);
        
        for(int n:arr)
            System.out.println(n);
	}//main
	
	//정렬하는 메소드
	static void sort(int[] a, int length) {
		
		//간격 (최소: 1, 최대: 배열의 길이보다 작아야 한다 + (부분집합 내 요소가 최소 2개)
		int h; 
		for(h=1; h<length/9 ;h=h*3+1);
		
		for(;h>0;h/=3) {
			
			for(int i=h; i<length; i++) { //정렬되지 않는 범위를 지정하는 변수i
				int start = a[i]; //정렬X범위의 첫 요소
				int j;
				//정렬된 범위를 지정해 이전 요소들과 비교 
				for(j=i-h; j>=0 && start<a[j]; j-=h) 
					a[j+h] = a[j];
				a[j+h] = start;
			}
		}		
	}//sort
}

 

'IT > 알고리즘' 카테고리의 다른 글

[백준] 1181  (2) 2022.09.19
[백준] 10817  (0) 2022.09.18
[프로그래머스] 1차 다트게임  (0) 2022.09.13
[백준] 9012번 괄호  (0) 2022.08.24
[백준] 9093번 단어 뒤집기  (0) 2022.08.22
Comments