Time Complexity ๐Ÿง

Seoyul Kimยท2020๋…„ 6์›” 29์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
1/4

์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€?

  • ์–ด๋– ํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๋™์ž‘๋“ค์˜ ๋ชจ์ž„์œผ๋กœ ์œ ํ•œ์„ฑ์„ ๊ฐ€์ง„๋‹ค.
  • ํ•œ ๋ฌธ์ œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐ€์ง€์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— time complexity๋ฅผ ์„ค๋ช…ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„๋ž€ input์— ๋Œ€ํ•˜์—ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์–ผ๋งˆ๋‚˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š”์ง€๋ฅผ ํ‘œํ˜„ํ•˜๋ฉฐ Big-0 ํ‘œ๊ธฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

Big-O Notations

Constant Time(์ƒ์ˆ˜ ์‹œ๊ฐ„)

  • O(1) : ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•  ๋•Œ ํ‚ค๋ฅผ ์•Œ๊ฑฐ๋‚˜ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๊ฐ’์„ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ์ž…๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ ์–‘๊ณผ ์ƒ๊ด€ ์—†์ด ์ผ์ •ํ•˜๊ฒŒ ์‹คํ–‰๋œ๋‹ค.

Logarithmic Time(๋กœ๊ทธ ์‹œ๊ฐ„)

  • O(log n) : ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋‹จ๊ณ„๋“ค์ด ์—ฐ์‚ฐ๋งˆ๋‹ค ํŠน์ • ์š”์ธ์— ์˜ํ•ด ์ค„์–ด๋“ ๋‹ค.

    ๐Ÿ‘‰๐Ÿป example
    ๋ฐฐ์—ด์—์„œ ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•  ๋•Œ ์–ด๋””์—์„œ ์‹œ์ž‘ํ• ์ง€ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ๊ฒ€์ƒ‰์‹œ๊ฐ„์ด ๋‘๋ฐฐ๋กœ ์ค„์–ด๋“ ๋‹ค.

  • O(n) : ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์–‘์— ๋”ฐ๋ผ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์ด ๋น„๋ก€ํ•œ๋‹ค.

Linearithmic Time(์„ ํ˜• ํƒ์ƒ‰, for ๋ฌธ)

  • O(n log n) : ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์–‘์ด n๋ฐฐ ๋งŽ์•„์งˆ์ˆ˜๋ก ์ˆ˜ํ–‰์‹œ๊ฐ„์€ n๋ฐฐ๋ณด๋‹ค ๋” ๋งŽ์•„์ง„๋‹ค.

Quadratic Time(์ง€์ˆ˜ ์‹œ๊ฐ„)

  • O(n^2) : ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์–‘์— ๋”ฐ๋ฅธ ์ˆ˜ํ–‰์‹œ๊ฐ„์€ ์ œ๊ณฑ์— ๋น„๋ก€ํ•œ๋‹ค.

    ๐Ÿ‘‰๐Ÿป ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ์กฐํ•ฉ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ


Ref)
https://joshuajangblog.wordpress.com/2016/09/21/time_complexity_big_o_in_easy_explanation/
https://www.holaxprogramming.com/2017/12/29/algorithms-learning-strategy/
https://towardsdatascience.com/understanding-time-complexity-with-python-examples-2bda6e8158a7
https://stackabuse.com/big-o-notation-and-algorithm-analysis-with-python-examples/

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