์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์‚ฝ์ž… ์ •๋ ฌ

2023. 11. 6. 09:34ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1241

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ๋ฒ„๋ธ” ์ •

https://hyejin.tistory.com/1240 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์„ ํƒ ์ • https://hyejin.tistory.com/1239 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch05. Stack, Queue (์ž

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

 

 

3. ์‚ฝ์ž… ์ •๋ ฌ 

์„ค๋ช…

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

์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์‚ฝ์ž…์ •๋ ฌ์ž…๋‹ˆ๋‹ค.

 

์ž…๋ ฅ

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

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

 

์ถœ๋ ฅ

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

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

6
11 7 5 6 10 9

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

5 6 7 9 10 11

 

 

๋ฌธ์ œ ํ’€์ด 1

public int[] solution(int n, int[] arr)
{
   for (int i = 1; i < n; i++)
   {
      int key = i;
      for (int j = i-1; j >= 0; j--)
      {
         if (arr[key] < arr[j]) {
            int tmp = arr[key];
            arr[key] = arr[j];
            arr[j] = tmp;
         }
         key--;
      }
   }
   
   return arr;
}

 

๐Ÿ‘ฉ‍๐Ÿ’ป : ์šฐ์„  ์‚ฝ์ž… ์ •๋ ฌ์ด๋ž€ i๊ฐ€ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ณ , j๋Š” i-1 ๋ถ€ํ„ฐ 0๊นŒ์ง€ ๋Œ๋ฉด์„œ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

๋‚˜๋Š” key ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  key์— ํ˜„์žฌ i ์˜ ๊ฐ’์„ ๋‹ด์•˜๋‹ค. 

๊ทธ ํ›„ ๋‘๋ฒˆ์งธ for๋ฌธ ์•ˆ์—์„œ arr[key]์™€ arr[j]์˜ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ arr[j]๊ฐ€ arr[key]๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ทธ ๊ฐ’์„ ๊ตํ™˜ํ•ด์คฌ๊ณ , key ์˜ ๊ฐ’์„ 1์”ฉ ๊ฐ์†Œ์‹œ์ผฐ๋‹ค. 

์ •๋ ฌํ•ด์ค€ ๋‹ค์Œ arr ๋ฅผ ๋ฆฌํ„ดํ–ˆ๋‹ค. 

 

 

 

 

๋ฌธ์ œ ํ’€์ด 2

public int[] solution2(int n, int[] arr)
{
   for (int i = 1; i < n; i++)
   {
      int tmp = arr[i], j;
      for ( j = i - 1; j >= 0; j--)
      {
         if (arr[j] > tmp)
         {
            arr[j + 1] = arr[j];
         }
         else
         {
            break;
         }
      }
      arr[j + 1] = tmp;
   }
   
   return arr;
}

 

๐Ÿ‘พ :  ๊ฐ•์‚ฌ๋‹˜์€ i๊ฐ€ 1์ผ๋•Œ, j๋Š” i-1 ์ฆ‰ 0 ๊นŒ์ง€ ๋Œ๋ฉด์„œ tmp ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  tmp์—๋Š” arr[i] ์˜ ๊ฐ’์„ ๋„ฃ์–ด๋‘๊ณ , arr[i]์™€ arr[j]์˜ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค. ์ด๋•Œ arr[j]๊ฐ€ tmp ๋ณด๋‹ค ์ž‘์œผ๋ฉด arr[j+1] ์— arr[j]์˜ ๊ฐ’์„ ๋„ฃ๊ณ  (์ž๋ฆฌ ๋ฐ”๊ฟˆ) ๋งŒ์•ฝ arr[j]๊ฐ€ tmp๋ณด๋‹ค ํฌ๋ฉด break ํ•ด์คฌ๋‹ค. break ํ•˜๋ฉด ๋‘๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์„ ๋‚˜์™€์„œ arr[j+1]์— tmp ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค. 

(๋‘๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์„ ๋๋‚ธ arr[j+1] = tmp๋Š” j๊ฐ€ -1 ์ผ๋•Œ๋กœ -1 + 1  ์ฆ‰, arr[0]์— tmp ๊ฐ’์„ ๋„ฃ๋Š” ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. )

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90

'์ธํ”„๋Ÿฐ > ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์ค‘๋ณต ํ™•์ธ  (0) 2023.11.07
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : Least Recently Used  (0) 2023.11.06
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ๋ฒ„๋ธ” ์ •๋ ฌ  (1) 2023.11.03
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์„ ํƒ ์ •๋ ฌ  (0) 2023.11.03
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch05. Stack, Queue (์ž๋ฃŒ๊ตฌ์กฐ) : ์‘๊ธ‰์‹ค  (0) 2023.11.02