本文へスキップ
製品デスクトップツール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製品情報は検証済みの開発ソースに基づいています。リリース・改善中

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

ソート ガイド · 2/6

How It Works

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

This chapter traces the classic sorting algorithms by hand on small arrays. After a quick look at three simple sorts, it walks through merge sort, quicksort and heapsort, the ones that matter most in practice and in interviews, and then counting sort, which never compares elements.

Three simple sorts

  • Bubble sort: compare neighbors and swap them if they are out of order. Each pass "bubbles" the largest remaining value to the end. Stop early if a pass makes no swaps.
  • Selection sort: find the smallest value in the unsorted part and swap it to the front. At most n - 1 swaps, but always about n²/2 comparisons.
  • Insertion sort: insert the next element into its place in the sorted prefix, the way you arrange a hand of cards.

All three are O(n²) on average, but insertion sort is close to O(n) on nearly sorted input and very fast on tiny arrays, so real library sorts use it for short ranges. Here is insertion sort on [6, 3, 8, 2, 5]:

StepInserted valueResult
i = 13[3, 6, 8, 2, 5]
i = 28[3, 6, 8, 2, 5]
i = 32[2, 3, 6, 8, 5]
i = 45[2, 3, 5, 6, 8]
def insertion_sort(a):
    for i in range(1, len(a)):
        key = a[i]
        j = i - 1
        while j >= 0 and a[j] > key:  # shift only strictly larger values: stable
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = key
    return a

print(insertion_sort([6, 3, 8, 2, 5]))  # [2, 3, 5, 6, 8]

Merge sort: split, then merge

Merge sort is the textbook divide-and-conquer algorithm. Split the array in half, sort each half recursively, then merge the two sorted halves. Merging repeatedly takes the smaller of the two front elements, so it costs time proportional to the total length. Sorting [38, 27, 43, 3, 9, 82, 10]:

PhaseState
Split[38, 27, 43] / [3, 9, 82, 10]
Split again[38] / [27, 43] , [3, 9] / [82, 10]
Merge small pieces[27, 38, 43] , [3, 9, 10, 82]
Final merge[3, 9, 10, 27, 38, 43, 82]

In the final merge, 27 is compared with 3 and 3 is taken; then 27 against 9, 27 against 10, 27 against 82, and so on. Taking the left element on ties keeps the sort stable. The recursion is levels deep and each level merges elements in total, giving .

この章について質問がありますか?
前の章Sorting Concepts次の章Implementation
log n
n
O(n log n)

Quicksort: partition around a pivot

Quicksort picks a pivot, partitions the array so that smaller values go left and larger values go right, then sorts both sides recursively. There is no merge step, and the work happens in place. With the simple Lomuto partition and pivot 3 (the last element), [5, 2, 8, 1, 9, 3] is processed like this, where i is the next slot for a small value:

ja[j]Less than 3?ActionArrayi
05nonone[5, 2, 8, 1, 9, 3]0
12yesswap with a[0][2, 5, 8, 1, 9, 3]1
28nonone[2, 5, 8, 1, 9, 3]1
31yesswap with a[1][2, 1, 8, 5, 9, 3]2
49nonone[2, 1, 8, 5, 9, 3]2
end--move pivot to a[2][2, 1, 3, 5, 9, 8]2

The pivot 3 now sits at its final index 2. Sorting [2, 1] and [5, 9, 8] the same way finishes the job.

def partition(a, lo, hi):
    pivot = a[hi]
    i = lo
    for j in range(lo, hi):
        if a[j] < pivot:
            a[i], a[j] = a[j], a[i]
            i += 1
    a[i], a[hi] = a[hi], a[i]
    return i

data = [5, 2, 8, 1, 9, 3]
print(partition(data, 0, len(data) - 1), data)  # 2 [2, 1, 3, 5, 9, 8]

If the pivot is always an extreme value (for example, the last element of an already sorted array), the split is n - 1 versus 0 and the running time becomes O(n²). Real implementations pick a random pivot or the median of three.

Heapsort: extract from a max-heap

Heapsort turns the array into a max-heap (a complete binary tree where each parent is at least as large as its children), then repeatedly swaps the root, the maximum, to the end and repairs the heap. The children of index i are 2i + 1 and 2i + 2. [4, 10, 3, 5, 1] becomes the heap [10, 5, 3, 4, 1], and then:

ExtractedAfter swap and sift down
10[5, 4, 3, 1, 10]
5[4, 1, 3, 5, 10]
4[3, 1, 4, 5, 10]
3[1, 3, 4, 5, 10]

Heapsort is O(n log n) in the worst case with O(1) extra memory, but it is not stable and its scattered memory access usually makes it slower than quicksort in practice.

Counting sort: count, then place

If the values are integers from 0 to k - 1, you can sort without comparisons. Count each value, turn the counts into prefix sums that mark where each value ends, then walk the input backwards and drop each element into place. Walking backwards makes it stable, which is what lets radix sort use it as a building block.

def counting_sort(a, k):
    count = [0] * k
    for x in a:
        count[x] += 1
    for v in range(1, k):
        count[v] += count[v - 1]  # prefix sum: one past the last slot of value v
    out = [0] * len(a)
    for x in reversed(a):
        count[x] -= 1
        out[count[x]] = x
    return out

print(counting_sort([3, 1, 2, 3, 0, 1], 4))  # [0, 1, 1, 2, 3, 3]

Radix sort applies this stable counting sort digit by digit, from the least significant to the most significant. Because each pass is stable, the order built by earlier passes survives.

Summary

  • Insertion sort shines on tiny or nearly sorted arrays, which is why bigger algorithms use it internally.
  • Merge sort splits and merges; taking the left element on ties keeps it stable.
  • Quicksort partitions around a pivot, and pivot choice decides its performance.
  • Heapsort is an in-place O(n log n) worst-case sort; counting sort sorts integers by counting instead of comparing.

コメント 0件

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

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