์๊ฐ ๋ณต์ก๋๋ ์๊ณ ๋ฆฌ์ฆ์ด ์ ๋ ฅ ๋ฐ์ดํฐ์ ํฌ๊ธฐ์ ๋ํด ์ผ๋ง๋ ํจ์จ์ ์ผ๋ก ๋์ํ๋์ง๋ฅผ ๋ํ๋ด๋ ๊ฐ๋ ์ ๋๋ค.


์ข ๋ ๊ตฌ์ฒด์ ์ผ๋ก๋ ์๊ณ ๋ฆฌ์ฆ์ด ์ ๋ ฅ ํฌ๊ธฐ์ ๋ฐ๋ผ ์์๋๋ ๊ณ์ฐ ์๊ฐ์ ์ฆ๊ฐ ์ ๋๋ฅผ ํํํฉ๋๋ค. ์๊ฐ ๋ณต์ก๋๋ ์๊ณ ๋ฆฌ์ฆ์ ํจ์จ์ฑ์ ๋ถ์ํ๊ณ ๋ค๋ฅธ ์๊ณ ๋ฆฌ์ฆ๊ณผ ๋น๊ตํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
ํ๋ก๊ทธ๋๋ฐ์์ ์๊ฐ๋ณต์ก๋๋ ํ๋ก๊ทธ๋๋จธ๊ฐ ์์ฑํ ์ฝ๋์ Running time์ ์๋ฏธํฉ๋๋ค.

์ 3์ค ์ง๋ฆฌ ์ฝ๋์ ์์ ์๊ฐ์ 7ns ์ ๋๋ค. ์ข์ ํ๋ก๊ทธ๋จ์ด๋ผ๋ฉด ์ค์ํ ์๋์ ์ ํ์ฑ์ด ๋ท๋ฐ์นจ๋์ด์ผ ํ๊ธฐ๋๋ฌธ์ ํ๋ก๊ทธ๋จ์ ์์ ์๊ฐ์ด ์ค์ํ๋ค๊ณ ๋งํ ์ ์์ต๋๋ค.

๊ทธ๋ผ ๋ฐ๋ณต๋ฌธ์ ๊ฒฝ์ฐ๋ ์ด๋จ๊น์?
- 'Outer loop'์ ๊ฒฝ์ฐ 5๋ฒ โ n๋ฒ์ผ ๊ฒฝ์ฐ n๋ฒ
- 'Inner loop'์ ๊ฒฝ์ฐ 25๋ฒ โ n๋ฒ์ผ ๊ฒฝ์ฐ n^2๋ฒ
= 5n^2 + 3n
์ด๋ ๊ฒ ์ด ์คํ์๊ฐ์ ์ ๋ ฅํฌ๊ธฐ n์ ๋ํ ํจ์๋ก ํํํ ๊ฒฐ๊ณผ๋ฅผ ์๊ฐ๋ณต์ก๋๋ผ ํฉ๋๋ค. ์ด๋, n์ ์ ๋ ฅ ๋ฐ์ดํฐ์ ํฌ๊ธฐ์ ๋๋ค. ๋ณต์กํ ์๊ฐ๋ณต์ก๋์ ํํ์ ๊ฐ๋จํ ์์๋ณด๊ธฐ ์ํด์ ํจ์์ ์ต๊ณ ์ฐจํญ์ผ๋ก๋ง ์๊ฐ๋ณต์ก๋๋ฅผ ํ๊ธฐํ ๊ฒ์ Big O ํ๊ธฐ๋ฒ์ด๋ผ๊ณ ํฉ๋๋ค, ์ด๋ ์ํ์ ๋ํ๋ด๋ฉฐ ์๊ณ ๋ฆฌ์ฆ์ ์คํ ์๊ฐ์ด ์ผ๋ง๋ ๋น ๋ฅด๊ฒ ์ฆ๊ฐํ๋์ง๋ฅผ ๋ํ๋ ๋๋ค.

์๋ฅผ ๋ค์ด, O(n), O(n log n), O(n^2)์ ๊ฐ๊ฐ ์ ํ ์๊ฐ, ๋ก๊ทธ ์ ํ ์๊ฐ, ์ด์ฐจ ์๊ฐ ๋ณต์ก๋๋ฅผ ๋ํ๋ ๋๋ค. ์ด๋ ์ ๋ ฅ ํฌ๊ธฐ n์ด ์ปค์ง ๋ ๊ฐ๊ฐ ์ ํ์ ์ผ๋ก, ๋ก๊ทธ ์ ํ์ ์ผ๋ก, ์ ๊ณฑ์ ์ผ๋ก ์ฆ๊ฐํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.

์๊ฐ ๋ณต์ก๋๋ฅผ ํ๊ฐํ ๋๋ ์ต์ ์๊ฐ ๋ณต์ก๋, ํ๊ท ์๊ฐ ๋ณต์ก๋, ์ต์ ์๊ฐ ๋ณต์ก๋ ๋ฑ ๋ค์ํ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ ์ ์์ต๋๋ค. ์ฃผ๋ก ์ต์ ์๊ฐ ๋ณต์ก๋๊ฐ ๊ฐ์ฅ ์ค์ํ๊ฒ ๋ค๋ฃจ์ด์ง๋ฉฐ, ์ด๋ ์ต์ ์ ์ํฉ์์๋ ์๊ณ ๋ฆฌ์ฆ์ด ์ด๋ ์ ๋์ ์ฑ๋ฅ์ ๋ณด์ฅํ๋์ง๋ฅผ ๋ํ๋ ๋๋ค.
Time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm.
Big O Notation is a tool used to describe the time complexity of algorithms. It calculates the time taken to run an algorithm as the input grows.
'Programming > C language' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
4 Constant (1) | 2024.01.23 |
---|---|
3 Variable (0) | 2024.01.22 |
2 Hello world (0) | 2024.01.22 |
1 C language - Why do we learn it (0) | 2024.01.22 |
[์๋ฃ๊ตฌ์กฐ] Data structure (0) | 2024.01.07 |
๋๊ธ