惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - Franky
WordPress大学
WordPress大学
Jina AI
Jina AI
Google DeepMind News
Google DeepMind News
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
博客园 - 【当耐特】
IT之家
IT之家
G
Google Developers Blog
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
V
Visual Studio Blog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
GbyAI
GbyAI
雷峰网
雷峰网

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Why Repeatedly Halving an Array is O(log n), Not Linear
Deep Coomer · 2026-06-25 · via DEV Community

When you first learn about Binary Search, the explanation usually goes something like this: "We look at the middle of the sorted array. If it's not our target, we throw away half the array and repeat."

It's an incredibly efficient strategy. But if you sit down and try to map that action to Big-O notation, an intuitive trap opens up:

"If we are breaking the array in half, shouldn't the time complexity be O(n/2)?"

If you’ve ever had this thought, you aren't alone. Today, let's look past the generic "just memorise the cheat sheet" advice and break down the exact math of why repeated halving completely changes the growth class of your code.

1. The Flaw in O(n/2)

The immediate, "textbook" answer to this question is that Big-O notation ignores constant multipliers. Because of this, O(n/2) isn't actually a distinct category:

O(n/2) = O(1/2 * n) = O(n)

An O(n/2) algorithm is still linear. If your dataset grows from 1,000 to 1,000,000 items, a linear algorithm's operations scale proportionally by 1,000x.

But when we search a sorted array by halving it, we aren't just dividing the work into two piles and scanning one entire pile. We are repeating the division process at every single step.

2. The Logic of Repeated Halving

Let's step away from the abstract formulas and look at a concrete example. Imagine we have a sorted array of 8 elements, and we are looking for a specific number.

Every time we check the middle element and miss, the search space shrinks:

  • Step 0: We start with 8 elements.
  • Step 1: First cut -> 4 elements left.
  • Step 2: Second cut -> 2 elements left.
  • Step 3: Third cut -> 1 element left (we either found it or it's not there).
[ 1, 2, 3, 4, 5, 6, 7, 8 ]  -> Step 0 (Size: 8)
      [ 5, 6, 7, 8 ]        -> Step 1 (Size: 4)
         [ 7, 8 ]           -> Step 2 (Size: 2)
           [ 8 ]            -> Step 3 (Size: 1)

It exactly took us 3 steps to reduce 8 elements down to 1.

3. Connecting the Dots to Logarithms

How do we mathematically describe the relationship between our starting size (8) and the number of steps it took to get to one (3)?

We are asking: "How many times do we need to multiply 2 by itself to reach 8?"

2 x 2 x 2 = 2^3 = 8

The inverse of an exponent is a logarithm. Specifically, a base-2 logarithm (log_2) answers the exact opposite question: "How many times do I have to divide this number by 2 to get down to 1?"

log_2(8) = 3

If we scale this up to an array of any size n, the number of steps k required to finish the search follows this formula:

n/2^k = 1 => n = 2^k => k = log_2(n)

4. Why Big-O Drops the Log Base

You might wonder why we write O(log n) instead of O(log_2 n), especially since we are dividing by 2. In computer science, base-2 is the default assumption, but mathematically, the base actually doesn't change the Big-O classification.

Thanks to the Change of Base rule, converting a base-2 log to a standard base-10 log looks like this:

log_2(n) = log_10(n) / log_10(2) ≈ 3.32 * log_10(n)

Because log_10(2) is just a constant number (≈ 0.301), log_2(n) and log_10(n) differ only by a constant multiplier (3.32).. Just like dropping the 1/2 from O(n/2), Big-O drops this multiplier too:

O(log_2 n) = O(3.32 * log_10 n) = O(log_n)

Whether you are dividing your data into 2 parts (Binary Search) or 10 parts (like a 10-way B-Tree search), the algorithm scales at the exact same logarithmic rate.

Summary Comparison

Concept Mechanics Big-O Class Scaling Behavior
O(n/2) A single scan over exactly half of the data. O(n) (Linear) If data doubles, execution time doubles.
O(\log n) Data is repeatedly cut in half at every step. O(\log n) (Logarithmic) If data doubles, you only add one extra step.

The Ultimate Perspective:
If you have a sorted array of 1,000,000 elements:

  • A linear O(n/2) approach takes 500,000 operations.
  • A logarithmic O(\log n) approach takes roughly 20 operations.

The next time someone tells you an algorithm is efficient because it "cuts the problem in half," remember that the magic isn't in the first cut—it's in the power of the repeated cut.

Thanks for reading! If you found this breakdown helpful, drop a comment below with the computer science concept that took you the longest to intuitively click.