๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

[์ •๋ ฌ] 7. ์ขŒํ‘œ ์ •๋ ฌ

์‹œํ๋ฆฌํ‹ฐ์ง€ํ˜ธ 2024. 1. 20.

<<ํ’€์ด>>

์ด ๋ฌธ์ œ๋ฅผ ์ ‘๊ทผํ•  ๋•Œ, ํด๋ž˜์Šค๋ฅผ ์ƒ๊ฐ์„ ํ–ˆ์—ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ๋ฐฐ์—ด์ด๋‚˜ ํด๋ž˜์Šค์— ๋„ฃ์–ด์„œ ์ •๋ ฌ์„ ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด์—์„œ๋Š” ์ฆ‰, ๊ธฐ๋ณธ ํƒ€์ž… ์ผ๋•Œ๋Š” Arrays.sort๋ฅผ ํ™œ์šฉํ•ด์„œ ์ •๋ ฌ์„ ํ–ˆ๋‹ค๋ฉด ๊ฐ์ฒด ํ˜•ํƒœ๋Š” ์–ด๋–ป๊ฒŒ ํ• ์ง€ ์˜๋ฌธ์ด ๋“ค์—ˆ๋‹ค. ๋น„๋ก ํ•˜๋”๋ผ๋„ ์—„์ฒญ ๋ณต์žกํ•  ๊ฒƒ ๊ฐ™์•˜๋‹ค. 

 

๊ฐ•์‚ฌ๋‹˜์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฌ๋‚˜ ํ–ˆ๋‹ค ใ…‹ใ…‹

 

-๊ฐ•์‚ฌ๋‹˜ ํ’€์ด-

import java.util.*;
class Point implements Comparable<Point> {
    public int x, y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    @Override
    public int compareTo(Point o) {
        if(this.x == o.x) return this.y - o.y;
        else return this.x - o.x;
    }
}

class Main{
    public static void main(String[] args) {
        Main T = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        ArrayList<Point> arr = new ArrayList<>();
        for(int i = 0; i < n; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            arr.add(new Point(x,y));
        }

        Collections.sort(arr);
        for(Point x : arr) System.out.println(x.x + " " + x.y);

    }
}

 

 

Comparable<T> ๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ ์ถ”์ƒ๋ฉ”์„œ๋“œ์ธ comepareTo๋ฅผ ์žฌ์ •์˜ ํ•˜์—ฌ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์žฌ์ •์˜ ํ•  ๋•Œ ์˜ค๋ฆ„์ฐจ์ˆœ ํ˜•ํƒœ๋ฅผ ์›ํ•œ๋‹ค๋ฉด ์™ผ์ชฝ์—๋Š” ํ˜„์žฌ ํด๋ž˜์Šค ๊ฐ’์„ ๋„ฃ๊ณ  ์˜ค๋ฅธ ์ชฝ์—๋Š” ๋น„๊ต ๋Œ€์ƒ์„ ๋„ฃ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฐ˜๋Œ€๋กœ ๋‚ด๋ฆผ ์ฐจ์ˆœ์„ ํ•  ๋•Œ์—๋Š” ์™ผ์ชฝ์— ๋น„๊ต๋Œ€์ƒ์„ ์˜ค๋ฅธ์ชฝ์— ํ˜„์žฌ ํด๋ž˜์Šค์˜ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๊ณ  Collection.sort์— ์ด ๊ฐ์ฒด๋“ค์„ ๋‹ด์€ ArrayList๋ฅผ ๋„ฃ์œผ๋ฉด list๊ฐ€ ์ •๋ ฌ์ด ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐœ๋…์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜ค๋Š˜ ์ฒ˜์Œ ์•Œ์•˜๊ณ  ์•„์ง ์ œ๋Œ€๋กœ ๋ชจ๋ฅด๊ฒ ๋‹ค ใ…‹ใ…‹ ๊ทธ๋ž˜์„œ ๋”ฐ๋กœ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค

 

<<์ถ”๊ฐ€ ๊ณต๋ถ€>>

 

Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ž€?

comparable ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ฐ์ฒด๊ฐ„์˜ ๋น„๊ต๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค. ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„(implements)ํ•œ ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ compareTo<T o> ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.

 

  @Override
    public int compareTo(Point o) {
        if(this.x == o.x) return this.y - o.y;
        else return this.x - o.x;
    }

 

 

*๊ธฐ๋ณธ์ ์œผ๋กœ wrapper ํด๋ž˜์Šค๋Š” ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

์ด ๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด๊ฐ’์„ ๊ฐ€์ง€๊ณ  Collection.sort()๋ฅผ ํ†ตํ•ด list ์•ˆ์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

*Arrays.sort() ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐฐ์—ด์•ˆ์— ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์„œ Arrays.sort()ํ•ด๋„ ์ •๋ ฌ์ด ๋œ๋‹ค.

 

import java.util.*;
class Point implements Comparable<Point> {
    public int x, y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    @Override
    public int compareTo(Point o) {
        if(this.x == o.x) return this.y - o.y;
        else return this.x - o.x;
    }
}

class Main{
    public static void main(String[] args) {
        Main T = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Point[] arr = new Point[n];
        for(int i = 0; i < n; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            arr[i] = new Point(x, y);

        }

        Arrays.sort(arr);
        for(Point x : arr) System.out.println(x.x + " " + x.y);

    }
}

 

 

 

์ •๋ฆฌํ•˜์ž๋ฉด Collection.sort๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌ Arrays.sort๋Š” ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  wrapper ํด๋ž˜์Šค๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ 

Comparable<T> ๋ฅผ implements ํ•˜๊ณ  ์žˆ๊ณ  ๋งŒ์•ฝ ์ƒˆ๋กœ์€ ํด๋ž˜์Šค๋ฅผ ์ •๋ คํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Comparable<T>๋ฅผ implements ํ•˜๊ณ  

comparTo๋ฅผ ์žฌ์ •์˜ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

์˜ค๋Š˜๋„ ๋ฐฐ์›Œ๊ฐ„๋‹ค~~

 

 

๋Œ“๊ธ€