本文へスキップ
製品デスクトップツールTASTECAD図面の確認TASTEFILESファイル管理TASTEFTPファイル転送TASTESSHリモートワークスペースTASTEZIP圧縮と展開TASTEWINMファイル管理開発ツールTASTECODERust 統合開発環境TASTESTUDIO統合開発・テスト環境TASTEETLデータ統合(ETL)サーバーTASTEREMOTEリモートデスクトップサーバーTASTEGEO空間情報サーバーTASTEMAILメールワークスペース投資TASTESTOCK株式自動売買デスクトップアプリはじめるダウンロードドキュメント購入すべての製品
マーケットプレイス
ニュース
技術ニュース今日の開発ニュース開発ドキュメントプログラミング言語RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWebフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngularサーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Expressアプリ・デスクトップ フレームワークTauriFlutterReact NativeElectronアルゴリズムアルゴリズムの基礎主要アルゴリズム発展アルゴリズム実務のアルゴリズム
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケースコミュニティガイド
協業技術移転ウェブサイト構築デスクトップアプリ構築リリース・決済パイプライン販売パートナーお問い合わせ
サービスリモートデスクトップインストールパッケージのカスタマイズ
TASTEDEV
製品

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらくTASTEWINM2つの画面で整理する、キーボード中心のファイルマネージャー

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

投資

TASTESTOCKいつ・何を。自分で決めたルールどおりに動く株式自動売買

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケース
ポリシー会社概要プライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
昨日
累計閲覧
商号
GXSOFT
代表者
CHOI YOUNG GU
事業者登録番号
853-44-00368
所在地
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
メール
whitecalvin@tastedev.net
個人情報保護責任者
CHOI YOUNG GU

© 2026 TASTEDEV製品情報は検証済みの開発ソースに基づいています。リリース・改善中

探索 ドキュメント
探索
Searching ConceptsHow It WorksImplementationComplexityPractice ProblemsSearching in the Real World
ディスカッション

探索 ガイド · 4/6

Complexity

この章は現在、英語でのみ提供しています。

Choosing a search method is not only about the cost of one lookup. You also have to weigh the cost of preparing the data, the cost of updates, memory use, and which kinds of questions the structure can answer at all. This chapter lays out the costs of linear search, binary search, parametric search, ternary search and hash lookup, and turns them into rules for picking one.

Linear search

  • Best case: the first element matches, one comparison, O(1).
  • Average case: with the target equally likely anywhere, about n / 2 comparisons, O(n).
  • Worst case: the target is last or missing, n comparisons, O(n).
  • Space: O(1) extra memory.

It needs no preparation and works on unsorted data. Because it reads contiguous memory in order, which caches love, it often beats binary search in practice for a few dozen elements or fewer.

Binary search

Each comparison halves the range, so the worst case is floor(log2 n) + 1 comparisons: 20 for a million elements, 30 for a billion.

  • Time: best O(1) (the closed-interval form can hit the target at the first midpoint), average and worst O(log n). The lower_bound form always runs close to log n steps.
  • Space: O(1) for the loop version, O(log n) for a recursive version because of the call stack.
  • Precondition: the data must be sorted, and sorting costs O(n log n).

Counting the steps makes the logarithmic growth visible.

def lower_bound_count(a, x):
    lo, hi, steps = 0, len(a), 0
    while lo < hi:
        steps += 1
        mid = (lo + hi) // 2
        if a[mid] < x:
            lo = mid + 1
        else:
            hi = mid
    return lo, steps


for n in (10, 1_000, 1_000_000):
    a = list(range(n))
    print(n, lower_bound_count(a, n - 1)[1])   # 3, 10, 20

When does sorting pay off?

With k queries, linear search costs k * n while sort-then-search costs n log n + k log n. Once the number of queries is well above log n, sorting wins. If you will search a dataset once or twice and then throw it away, do not sort it.

Inserting into or deleting from a sorted array means shifting the elements behind the position, which is O(n). When updates are frequent, a balanced binary search tree, a B-tree, or a library such as Python's sortedcontainers keeps both lookups and updates near O(log n).

Parametric and ternary search

If the answer range has size R and evaluating the predicate costs , parametric search costs . A predicate that scans the whole array to test feasibility gives . On real numbers, run iterations for a precision , or simply a fixed count such as 100.

この章について質問がありますか?
前の章Implementation次の章Practice Problems
C
O(C * log R)
O(n log R)
log2(R / eps)
eps

Ternary search keeps two thirds of the range per round and evaluates the function twice each time. For the same precision it therefore needs more evaluations than binary search. If the problem is on integers and can be rewritten as a neighbor-comparison binary search, prefer that.

import math

R, eps = 1e9, 1e-6
print(math.ceil(math.log2(R / eps)))            # binary: about 50 rounds, 50 evaluations
print(math.ceil(math.log(R / eps, 1.5)) * 2)    # ternary: about 86 rounds, about 172 evaluations

Hash lookup

A hash table finds a key in O(1) on average, but if every key lands in the same bucket the worst case degrades to O(n). It also uses more memory than an array, and since it keeps no order it cannot answer range queries, "next larger value", or "k-th smallest".

import random
import timeit
from bisect import bisect_left

data = sorted(random.sample(range(10_000_000), 100_000))
as_set = set(data)
probe = data[len(data) // 2]

print(timeit.timeit(lambda: probe in data, number=200))     # linear: slowest
print(timeit.timeit(lambda: bisect_left(data, probe), number=200))
print(timeit.timeit(lambda: probe in as_set, number=200))   # hash: fastest

The exact numbers depend on your machine, but with 100,000 elements linear search is typically thousands of times slower than the other two, or worse.

Comparison table

MethodPreparationOne lookupInsert / deleteRange and order queriesExtra memory
Linear searchnoneO(n)O(1) (append)possible, but O(n)O(1)
Sorted array + binary searchO(n log n)O(log n)O(n)O(log n + k)O(1)
Balanced BST / B-treeO(n log n)O(log n)O(log n)O(log n + k)O(n)
Hash tableO(n)O(1) averageO(1) averagenot supportedO(n)

Here k is the number of results that fall inside the range.

Summary

  • Linear search is O(n) but needs no preparation, which suits small data and one-off lookups.
  • Binary search is O(log n); the O(n log n) sorting cost is recovered when there are many queries.
  • Parametric search costs O(predicate cost * log range); ternary search needs more evaluations for the same precision.
  • Use a hash for exact matches; use a sorted array or a tree when you need ranges or order.

コメント 0件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。