[ 221013 ]

์ตœ์ˆ˜์ •ยท2022๋…„ 10์›” 13์ผ
0

๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ

๋ชฉ๋ก ๋ณด๊ธฐ
10/14
post-thumbnail

๐Ÿ–ฅ๏ธ Algorithm

๋ฒ„๋ธ”์ •๋ ฌ

  • ์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ ฌํ•˜๋ฏ€๋กœ O(n^2)
  • ์ •๋ ฌ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์—์„  ์ œ์ผ ๋น„ํšจ์œจ์ ์ธ ์ •๋ ฌ์ด๋‹ค.
  • ์ œ์ž๋ฆฌ ์ •๋ ฌ(in-place sorting) : ์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐฐ์—ด ์•ˆ์—์„œ ๊ตํ™˜ํ•˜๋Š” ๋ฐฉ์‹์ด๋ฏ€๋กœ, ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š๋‹ค.
public class Bubblesort {
    public int [] sort(int [] arr) {

        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if ( arr[i] > arr[j] ) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }

    public static void main(String[] args) {

        int [] arr = new int[]{7, 2, 3, 9, 28, 11};

        Bubblesort bubblesort = new Bubblesort();

        System.out.print(Arrays.toString(bubblesort.sort(arr)));

    }
}

โŒ ์•ˆ์ชฝ for๋ฌธ์—์„œ int j = 1 ๋กœ ์„ค์ •ํ•˜๊ณ  for๋ฌธ์„ ๋Œ๋ ค์„œ ์›ํ•˜๋Š” ๊ฐ’์ด ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค.
โญ•int j = i+1๋กœ ๋ฐ”๊ฟ”์ฃผ๋‹ˆ ์›ํ•˜๋Š” ๊ฐ’์ด ๋‚˜์™”๋‹ค.
ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•œ ์ฝ”๋“œ๋Š” i๋ฒˆ์งธ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์žก๊ณ  j๋กœ ๋ฐฐ์—ด์•ˆ์„ loop๋ฅผ ๋Œ๋ฆฌ๋ฉฐ ํ•˜๋‚˜์”ฉ ๋น„๊ต/๋ณ€๊ฒฝ์„ ํ•˜๋ฉฐ i ํ•œ๋ฒˆ์ด ๋๋‚˜๋ฉด ๋ฐฐ์—ด์•ˆ์˜ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์ด arr[i]์— ๋‚จ๋Š” ๋ฐฉ์‹์ธ๋ฐ, j์˜ ์‹œ์ž‘์„ ์ƒ์ˆ˜๋กœ ์žก์•„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ์ •๋ ฌ์ด ์™„๋ฃŒ๋œ ๋ถ€๋ถ„๊นŒ์ง€๋„ ๊ฑด๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.

๐Ÿ–ฅ๏ธ parser ์‹ค์Šต

gradle์˜ test ๊ธฐ๋Šฅ ์‚ฌ์šฉํ•˜๊ธฐ

TDD๋ž€?
1. ์š”๊ตฌ์‚ฌํ•ญ ๋ถ„์„์„ ํ•œํ›„
2. ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๊ฒŒ Test๋ฅผ ๋จผ์ € ๋งŒ๋“ ๋‹ค.
3. Test์— ํ†ต๊ณผํ•˜๋Š” ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
๋‹จ์ : ์ผ์„ ๋‘๋ฒˆ ํ•œ๋‹ค (ํ…Œ์ŠคํŠธ + ์‹ค์ œ)

test ๋ž€?
์šฐ๋ฆฌ๊ฐ€ ์ฝ”๋“œ ์ˆ˜์ •ํ• ๋•Œ ๋งˆ๋‹ค Main์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด์คฌ๋˜๊ฒƒ์„ ๋Œ€์‹ ์— test๋กœ ์ฝ”๋“œ ์ˆ˜์ •๊ณผ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ ๊ธฐ๋Šฅ์ด๋‹ค.

  • ์œ ๋‹›ํ…Œ์ŠคํŠธ : ๊ธฐ๋Šฅ๋ณ„๋กœ ํ…Œ์ŠคํŠธ
  • ํ†ตํ•ฉํ…Œ์ŠคํŠธ : ๋ฐฐํฌ ์ „ ํ…Œ์ŠคํŠธ
  1. test ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์‹คํ–‰ํ•ด๋ณธ๋‹ค.

์‹ค์Šต ์ฐธ๊ณ 

์˜ค๋ฅ˜ ๊ด€๋ จ ๋งํฌ

  • ํ•œ๊ธ€๊นจ์ง

https://myhappyman.tistory.com/52

https://hashcode.co.kr/questions/4296/intellij%EC%97%90%EC%84%9C-txt%ED%8C%8C%EC%9D%BC-%EB%82%B4%EC%9A%A9%EC%9D%84-%EB%B0%9B%EC%95%84%EC%98%AC%EB%95%8C-%ED%95%9C%EA%B8%80%EC%9D%B4-%EC%A0%84%EB%B6%80%EB%8B%A4-%EA%B9%A8%EC%A0%B8%EC%84%9C-%EB%82%98%EC%98%B5%EB%8B%88%EB%8B%A4

https://yanggyu.tistory.com/3

0๊ฐœ์˜ ๋Œ“๊ธ€