Time complexity and Space complexity

2025. 8. 11. 23:49·Computer Science/Data Structure

Time Complexity

Time complexity describes how the number of operations grows as the input size N increases.

It measures how much time an algorithm takes, independent of hardware or programming language.

We focus on the number of operations, not exact timings.

To express it, we use Big-O notation, which reflects the most significant term in the function T(n) — the one that dominates as n grows.

 

 

Common Time Complexities:

 

  • O(1): constant time
  • O(log n): logarithmic time
  • O(n): linear time
  • O(n log n): linearithmic time (e.g., efficient sorting)
  • O(n^2): quadratic time
  • O(2^n): exponential time
  • O(n!): *factorial time

*Factorial

The factorial of a natural number n is the product of all natural numbers from 1 to n

 

Why does this matter?

 

Time complexity helps us write more efficient code.

For example, improving an O(n^2) algorithm to O(n) can reduce execution from 9 seconds to 3 seconds for large inputs.

 

 

How to determine time complexity

  • If a single loop iterates over a collection of n elements → O(n)
  • If it iterates over half or more of the collection → O(n / 2) which simplifies to O(n)
  • If you have two separate loops iterating over two different collections → O(n + m) which is still linear in total
  • If you have two nested loops iterating over a single collection → O(n²)
  • If you have two nested loops iterating over two different collections → O(n × m) — often written as O(n²) when both collections are of size n
  • If you use a sorting algorithm on a collection → O(n log n) (common for efficient comparison-based sorts like Merge Sort or Quick Sort)

 

Space Complexity

Space complexity refers to the amount of memory (space) an algorithm uses while running.

It includes:

  • Memory for variables
  • Memory used by recursion
  • Memory allocated dynamically during execution

In modern systems, memory has become cheaper and more abundant, so space complexity is usually less critical than time complexity — but still important in some cases.

'Computer Science > Data Structure' 카테고리의 다른 글

Linear and Nonlinear Data Structures(In progress)  (0) 2025.09.02
'Computer Science/Data Structure' 카테고리의 다른 글
  • Linear and Nonlinear Data Structures(In progress)
JTB
JTB
웹/앱 개발 정보를 공유하고 있습니다.
  • JTB
    JTechBlog
    JTB
  • 전체
    오늘
    어제
    • All About Programming;)
      • Computer Science
        • Terminology and Concepts
        • Network
        • Operating System
        • Database
        • Data Structure
        • Web Development
      • Frontend
        • Javascript Essentials
        • Perfomance Optimization
        • JS Patterns
        • React
        • Next.js
        • Flutter
        • Testing
      • Backend
        • Node.js
      • DevOps
        • Docker & Kubernetes
      • Coding Test
        • LeetCode
        • Programmers
      • Tech Books & Lectures
        • Javascript_Modern JS Deep d..
        • Network_IT 엔지니어를 위한 네트워크 입문
      • Projects
        • PolyLingo_2025
        • Build Your Body_2024
        • JStargram_2021
        • Covid19 Tracker_2021
        • JPortfolio_2021
      • BootCamp_Codestates
        • TIL
        • TILookCloser
        • Pre Tech Blog
        • IM Tech Blog
        • Daily Issues and DeBugging
        • First Project
        • Final Project
        • Sprint Review
        • Good to Know
        • Socrative Review
        • HTML & CSS
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 글쓰기
    • 관리
  • 공지사항

  • 인기 글

  • 태그

    Threads and Multithreading
    VoiceJournal
    polylingo
    database
    indie hacker
    Time complexity and Space complexity
    Network
    Shared resources
    이벤트
    프론트엔드 성능 최적화 가이드
    TCP/IP
    모던 자바스크립트 Deep Dive
    mobile app
    자바스크립트
    스코프
    딥다이브
    Binary Tree BFS
    leetcode
    How memory manage data
    DOM
    커리어
    structure of os
    need a database
    testing
    자바스크립트 딥다이브
    js pattern
    Javascript Essentials
    Operating System
    CPU scheduling algorithm
    Data Structure
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
JTB
Time complexity and Space complexity
상단으로

티스토리툴바