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;) N
      • Computer Science
        • Terminology and Concepts
        • Network
        • Operating System
        • Database
        • Data Structure
      • Frontend
        • Javascript Essentials
        • Perfomance Optimization
        • JS Patterns
        • Next.js
        • Flutter
      • Backend
        • Node.js
      • DevOps
        • Docker & Kubernetes
      • Coding Test N
        • LeetCode N
        • 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
  • 블로그 메뉴

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

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

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바