본문 바로가기
카테고리 없음

[JAVA] 정렬 알고리즘(sorting algorithm) - 달팽이 정렬 만들기(Snail Sort)

by 짜장이누나 2014. 7. 20.

Snail Sort


달팽이 정렬이 뭐지???????

그림으로 원리를 알아보자. 아주 쉽져잉



>> 소스보기

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Snail {
 
    public static void main(String[] args) {
        
        draw(3);
        draw(6);
        draw(9);
    }
 
    private static void draw(int size) {
        
        int input = size;
        int i, j, m, n;
        int cnt = 1;
        int right = 0, bottom = -1, top = 1;
        int[][] snailArr = new int[size][size];
        
        for(i=input; i>=0; i--) { //4
            
            for(j=0; j<input; j++) {
                bottom = bottom + top;
                snailArr[right][bottom] = cnt;
                cnt++;
            }
            
            input--//3
            
            for(j=0; j<input; j++) {
                right = right + top;
                snailArr[right][bottom] = cnt;
                cnt++;
            }
            
            top = top*(-1); // -1
        }
        
        for(m=0; m<size; m++) {
            for(n=0; n<size; n++) {
                System.out.print(snailArr[m][n] + "\t");
                
            }
            System.out.println("");
        }
        System.out.println("");
    }
    
}
cs




>> 결과보기