Skip to main content
ProductsDesktop toolsTASTECADDrawing reviewTASTEFILESFile managementTASTEFTPFile transferTASTESSHRemote workspaceTASTEZIPCompression and extractionTASTEWINMFile managementDeveloper toolsTASTECODERust integrated development environmentTASTESTUDIOIntegrated development and test environmentTASTEETLData integration (ETL)ServersTASTEREMOTERemote desktop serverTASTEGEOGeospatial serverTASTEMAILMail workspaceInvestingTASTESTOCKStock auto-trading desktop appGet startedDownloadsDocumentationBuyAll products
Marketplace
News
TechNewsToday's dev newsDeveloper docsProgramming languagesRustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngularServer frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · ExpressApp & desktop frameworksTauriFlutterReact NativeElectronAlgorithmsAlgorithm basicsCore algorithmsAdvanced algorithmsPractical algorithms
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcaseCommunity guide
Work with usTech transferWebsite buildDesktop app buildRelease and payment pipelineSales partnerContact
ServicesRemote desktopCustom installer packages
TASTEDEV
Products

Desktop tools

TASTECADComplex drawings, opened with easeTASTEFILESJust the files you need, as fast as possibleTASTEFTPThe tidiest path for your filesTASTESSHA stronger link between you and your serversTASTEZIPPack faster, unpack saferTASTEWINMA keyboard-first file manager with two panes

Developer tools

TASTECODEEdit, build, debug and test Rust apps in one windowTASTESTUDIOFrom editing to remote tests and approved AI fixesTASTEETLMove data while you watch the values

Servers

TASTEREMOTEA remote desktop you run on your own serverTASTEGEORebuilding a GeoServer-compatible geospatial server in RustTASTEMAILTake back control of your email

Investing

TASTESTOCKStock auto-trading that follows the rules you set: when, and what

Get started

DownloadsLatest builds of every productDocsHow to use each productBuySubscriptions and perpetual licencesAll productsEvery product at a glance
All products
MarketplaceNews
Tech

News

Today's dev newsOfficial announcements on languages, frameworks, GitHub and AIDeveloper docsGuides to languages, frameworks and algorithms

Programming languages

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

Frameworks

Web front-end frameworksNext.jsReactVue.js · NuxtSvelteKitAngular
Server frameworksSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
App & desktop frameworksTauriFlutterReact NativeElectron

Algorithms

Algorithm basicsComplexity analysis · Sorting · Searching · Basic data structures · Trees and heapsCore algorithmsGraph traversal · Shortest paths & minimum spanning trees · Dynamic programming · Greedy algorithms · Divide and conquer · BacktrackingAdvanced algorithmsString algorithms · Math and number theory · Bit manipulation · Segment trees · Union-findPractical algorithmsCoding interview strategy · LRU cache · Rate limiting · Consistent hashing
All development docs
Community
ProjectsDiscover builders and their workDevelopment logsDecisions and lessons learnedPromptsReusable instructionsAI tool issuesProblems and workarounds for Claude, Codex, GeminiShowcaseFinished resultsCommunity guideHow we build in the open
Work with us
Tech transferNative apps in Rust and eguiWebsite build10 languages, accounts, docs and downloadsDesktop app buildRust and egui, no WebViewRelease and payment pipelineSigning, MSI, deb, rpm, AppImage, hashesSales partnerRecommend us and earn 30%ContactBuilds, partnerships and investment
Services
Remote desktopReach your computer from a browserCustom installer packagesOnly the features you need, settings preloaded
Buy
EN
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
Log in
Log in
TASTEDEV

From small tools to better development.

support@tastedev.netSupport TASTEDEV
ProductsProduct lineupBuyDocsDownloadsMarketplaceSupport
CommunityProjectsDevelopment logsPromptsAI tool issuesShowcase
PoliciesAbout usPrivacy policyTerms of serviceSubscription termsRefund policyCommunity guidelines
AccountLog inSign up
Visitors
Today
Yesterday
Total views
Company
GXSOFT
Representative
CHOI YOUNG GU
Business registration number
853-44-00368
Address
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
Phone
+82-10-4404-1523
Email
whitecalvin@tastedev.net
Privacy officer
CHOI YOUNG GU

© 2026 TASTEDEVProduct information is based on verified development sources.Released · improving

Complexity analysis docs
Complexity analysis
Complexity Analysis ConceptsHow It Works: From Counting Operations to the Master TheoremImplementation: Measuring Three Growth RatesComplexity: Common Classes, Amortized Cost and SpacePractice ProblemsReal-World Use
Discussions

Complexity analysis guide · 3/6

Implementation: Measuring Three Growth Rates

We write O(n) linear search, O(log n) binary search and O(n²) pair counting in Python, time them with timeit, then port an O(n²) and an O(n) two-sum to C++, Java and TypeScript.

Python

import timeit

def linear_search(items, target):          # O(n)
    for i, x in enumerate(items):
        if x == target:
            return i
    return -1

def binary_search(items, target):          # O(log n), sorted input
    lo, hi = 0, len(items)
    while lo < hi:
        mid = (lo + hi) // 2
        if items[mid] < target:
            lo = mid + 1
        else:
            hi = mid
    if lo < len(items) and items[lo] == target:
        return lo
    return -1

def count_pairs_with_sum(items, target):   # O(n²)
    count = 0
    n = len(items)
    for i in range(n):
        for j in range(i + 1, n):
            if items[i] + items[j] == target:
                count += 1
    return count
  • linear_search scans from the front: worst case Θ(n).
  • binary_search keeps the range [lo, hi) and discards half of it per iteration, about log₂ n + 1 times. Then lo is the first index not less than the target (like bisect.bisect_left).
  • count_pairs_with_sum checks every pair i < j: n(n-1)/2 comparisons, Θ(n²).

Timing with timeit

timeit runs code many times with garbage collection off. Use repeat and keep the minimum; the rest is mostly noise.

def bench(func, *args, number=100, repeat=7):
    timer = timeit.Timer(lambda: func(*args))
    return min(timer.repeat(repeat=repeat, number=number)) / number

for n in (1_000, 2_000, 4_000):
    data = list(range(n))
    lin = bench(linear_search, data, -1)        # worst case
    bi = bench(binary_search, data, -1)
    pairs = bench(count_pairs_with_sum, data, -1, number=1, repeat=5)
    print(n, f"{lin:.2e} {bi:.2e} {pairs:.2e}")

Sample run:

nLinearBinaryPairs
1,00035.6 µs0.78 µs26.7 ms
2,00075.7 µs0.99 µs115.2 ms
4,000164.2 µs0.91 µs528.1 ms

Doubling n doubles linear search, quadruples pair counting, and binary search stays flat. This doubling experiment is the simplest check: a ratio of 2 means n, 4 means n², 8 means n³.

One problem, two complexities: two-sum

Find two indices whose values sum to target. Checking every pair is O(n²); a hash map of seen values makes it one O(n) pass on average.

def two_sum_quadratic(nums, target):
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] == target:
                return i, j
    return None

def two_sum_linear(nums, target):
    seen = {}                        # value -> first index
    for j, x in enumerate(nums):
        i = seen.get(target - x)
        if i is not None:
            return i, j
        seen.setdefault(x, j)
    return None

nums = [8, 3, 11, 5, 2]
print(two_sum_quadratic(nums, 13), two_sum_linear(nums, 13))  # (0, 3) (0, 3)

With several answers, the pairs found may differ.

C++

#include <iostream>
#include <optional>
#include <unordered_map>
#include <utility>
#include <vector>

using Pair = std::optional<std::pair<int, int>>;

// O(n^2)
Pair twoSumQuadratic(const std::vector<int>& nums, int target) {
    for (std::size_t i = 0; i < nums.size(); ++i)
        for (std::size_t j = i + 1; j < nums.size(); ++j)
            if (nums[i] + nums[j] == target) return std::make_pair(int(i), int(j));
    return std::nullopt;
}

// O(n) average
Pair twoSumLinear(const std::vector<int>& nums, int target) {
    std::unordered_map<int, int> seen;  // value -> first index
    for (std::size_t j = 0; j < nums.size(); ++j) {
        auto it = seen.find(target - nums[j]);
        if (it != seen.end()) return std::make_pair(it->second, int(j));
        seen.emplace(nums[j], int(j));
    }
    return std::nullopt;
}

int main() {
    if (auto r = twoSumLinear({8, 3, 11, 5, 2}, 13)) std::cout << r->first << ' ' << r->second << '\n';  // 0 3
}

Java

import java.util.HashMap;
import java.util.Map;

public class TwoSum {
    // O(n^2)
    static int[] twoSumQuadratic(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++)
            for (int j = i + 1; j < nums.length; j++)
                if (nums[i] + nums[j] == target) return new int[] {i, j};
        return null;
    }

    // O(n) average
    static int[] twoSumLinear(int[] nums, int target) {
        Map<Integer, Integer> seen = new HashMap<>();  // value -> first index
        for (int j = 0; j < nums.length; j++) {
            Integer i = seen.get(target - nums[j]);
            if (i != null) return new int[] {i, j};
            seen.putIfAbsent(nums[j], j);
        }
        return null;
    }

    public static void main(String[] args) {
        int[] r = twoSumLinear(new int[] {8, 3, 11, 5, 2}, 13);
        System.out.println(r[0] + " " + r[1]);  // 0 3
    }
}

TypeScript

// O(n^2)
function twoSumQuadratic(nums: number[], target: number): [number, number] | null {
  for (let i = 0; i < nums.length; i++)
    for (let j = i + 1; j < nums.length; j++)
      if (nums[i] + nums[j] === target) return [i, j];
  return null;
}

// O(n) average
function twoSumLinear(nums: number[], target: number): [number, number] | null {
  const seen = new Map<number, number>(); // value -> first index
  for (let j = 0; j < nums.length; j++) {
    const i = seen.get(target - nums[j]);
    if (i !== undefined) return [i, j];
    if (!seen.has(nums[j])) seen.set(nums[j], j);
  }
  return null;
}

const nums = [8, 3, 11, 5, 2];
console.log(twoSumQuadratic(nums, 13), twoSumLinear(nums, 13)); // [ 0, 3 ] [ 0, 3 ]

Summary

  • In a doubling experiment, O(n), O(log n) and O(n²) show up as about 2x, flat and about 4x.
  • Time with timeit; keep the minimum of several runs.
  • Two-sum trades O(n) memory to turn O(n²) into O(n).
Questions about this chapter?
Previous chapterHow It Works: From Counting Operations to the Master TheoremNext chapterComplexity: Common Classes, Amortized Cost and Space

0 comments

Top commentsNewest first

Sign in · Sign in to leave a comment.