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

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらく

開発ツール

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

サーバー

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

はじめる

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

ニュース

今日の開発ニュース言語・フレームワーク・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ツールの問題ショーケース
ポリシープライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
012345678901234567890123456789012345678901234567890123456789
昨日
012345678901234567890123456789012345678901234567890123456789
累計閲覧
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
商号
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製品情報は検証済みの開発ソースに基づいています。リリース・改善中

ソート ドキュメント
ソート
Sorting ConceptsHow It WorksImplementationComplexityPractice ProblemsReal-World Use
ディスカッション

ソート ガイド · 4/6

Complexity

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

Time complexity alone does not pick a sorting algorithm. You also need the gap between best, average and worst case, extra memory, stability, and sensitivity to input patterns. This chapter tabulates the main sorts, shows why comparison sorting cannot beat n log n, and explains how counting and radix sort sidestep that bound.

Comparison table

AlgorithmBestAverageWorstExtra memoryStableIn-place
Bubble sort (early exit)O(n)O(n²)O(n²)O(1)yesyes
Selection sortO(n²)O(n²)O(n²)O(1)noyes
Insertion sortO(n)O(n²)O(n²)O(1)yesyes
Merge sortO(n log n)O(n log n)O(n log n)O(n)yesno
Quicksort (random pivot)O(n log n)O(n log n)O(n²)O(log n) stacknoyes
HeapsortO(n log n)O(n log n)O(n log n)O(1)noyes
TimsortO(n)O(n log n)O(n log n)O(n)yesno
IntrosortO(n log n)O(n log n)O(n log n)O(log n)noyes
Counting sortO(n + k)O(n + k)O(n + k)O(n + k)yesno
Radix sortO(d(n + b))O(d(n + b))O(d(n + b))O(n + b)yesno

Here k is the value range, d the number of digits and b the base (values per digit). Quicksort's O(log n) stack holds only when the implementation recurses on the smaller side; naive recursion on both sides can go O(n) deep.

Analyzing merge sort and quicksort

Merge sort follows the recurrence T(n) = 2T(n/2) + O(n). Halving gives log n levels, each level merges n elements in total, so the cost is O(n log n). The split ignores the data, so best and worst case are the same.

Quicksort depends on how evenly the pivot splits the array. Even splits give O(n log n); if one side is always empty, the cost is n + (n - 1) + ... + 1 = O(n²). With a random pivot the expected number of comparisons is about 1.39 n log₂ n, a bit more than merge sort, but quicksort scans contiguous memory in place and is usually faster in practice. You can measure how badly a naive "first element as pivot" quicksort does on sorted input:

import random

def quicksort_count(a, choose):
    comparisons = 0
    stack = [(0, len(a) - 1)]
    while stack:
        lo, hi = stack.pop()
        if lo >= hi:
            continue
        p = choose(lo, hi)
        a[p], a[hi] = a[hi], a[p]
        pivot, i = a[hi], lo
        for j in range(lo, hi):
            comparisons += 1
            if a[j] < pivot:
                a[i], a[j] = a[j], a[i]
                i += 1
        a[i], a[hi] = a[hi], a[i]
        stack += [(lo, i - 1), (i + 1, hi)]
    return comparisons

n = 2000
print(quicksort_count(list(range(n)), lambda lo, hi: lo))               # 1999000 = n(n-1)/2
print(quicksort_count(list(range(n)), lambda lo, hi: random.randint(lo, hi)))  # roughly 25000

The Ω(n log n) lower bound

A comparison sort learns the order only through yes/no questions of the form "is a < b?". Drawing every possible sequence of questions gives a decision tree whose leaves correspond to permutations of the input. There are n! orderings of n distinct elements, so the tree needs at least n! leaves, and a binary tree of height h has at most 2^h leaves. Hence 2^h ≥ n!, so h ≥ log₂(n!). Since n! is at least (n/2)^(n/2), we get log₂(n!) ≥ (n/2) log₂(n/2), and the worst-case number of comparisons is Ω(n log n).

This means merge sort and heapsort are asymptotically optimal. It also means that dropping the "comparisons only" assumption can buy speed.

import math

for n in [10, 100, 1000]:
    lower = math.ceil(math.log2(math.factorial(n)))
    print(n, lower, round(n * math.log2(n)))
# 10 22 33
# 100 525 664
# 1000 8530 9966

Sorting without comparisons

Counting sort runs in O(n + k) when the value range k is comparable to n or smaller. Ages, scores from 0 to 100 and byte values are good fits; when k is huge, time and memory follow k.

Radix sort splits w-bit integers into r-bit digits and runs d = w / r stable counting passes. For 32-bit integers with 8-bit digits, d = 4 and b = 256, so the cost is 4(n + 256), effectively linear. Fixed-length strings can be sorted the same way.

def radix_sort(a, bits=32, r=8):
    mask = (1 << r) - 1
    for shift in range(0, bits, r):
        buckets = [[] for _ in range(1 << r)]
        for x in a:                      # appended in order, so each pass is stable
            buckets[(x >> shift) & mask].append(x)
        a = [x for bucket in buckets for x in bucket]
    return a

print(radix_sort([170, 45, 75, 90, 802, 24, 2, 66]))
# [2, 24, 45, 66, 75, 90, 170, 802]

This version handles non-negative integers only; negatives need the sign bit flipped or a separate pass.

Summary

  • Merge sort and heapsort are O(n log n) in the worst case; quicksort is fast on average but needs a good pivot strategy to avoid O(n²).
  • The decision-tree argument proves no comparison sort beats Ω(n log n).
  • Counting and radix sort exploit integer structure to approach linear time, paying in range limits and memory.
  • Real choices also weigh stability, extra memory and adaptivity to nearly sorted input.
この章について質問がありますか?
前の章Implementation次の章Practice Problems

コメント 0件

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

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