メインコンテンツへスキップ
Z

Sorting Algorithms

Bubble, insertion, merge, quick & heap sort — compared and visualized

Sorting is the classic lens for learning algorithm analysis: every method produces the same ordered output, but the cost of getting there ranges from O(n²) to O(n log n). Watching them side by side makes the trade-offs concrete.

The simple O(n²) sorts

Bubble sort repeatedly swaps adjacent out-of-order pairs; selection sort repeatedly picks the smallest remaining element; insertion sort grows a sorted prefix one element at a time. All three are quadratic in the worst case, but insertion sort shines on small or nearly-sorted inputs, where it approaches O(n).

The divide-and-conquer sorts

Merge sort splits the array in half, sorts each half and merges them, guaranteeing O(n log n) at the cost of extra memory. Quicksort partitions around a pivot and recurses; it is usually the fastest in practice thanks to cache locality, but a poor pivot can degrade it to O(n²). Heap sort builds a binary heap and repeatedly extracts the max, sorting in place in O(n log n) with no extra memory.

Stability & in-place

Two properties decide which sort fits a job: stability (equal keys keep their original order — merge and insertion sort are stable; quick and heap sort are not) and whether the sort works in place (no significant extra memory — quick and heap sort do; merge sort does not). Step through each below to feel the difference.

Try it interactively

バブルソート ビジュアライザー

バブルソートの動きをアニメーションで確認できるツール。ステップ実行や速度調整、カスタム入力データ、比較・交換回数のリアルタイム表示、擬似コードの表示に対応しています。すべてブラウザ内だけで動作します。

ツールを開く

挿入ソート(Insertion Sort)ビジュアライザー

挿入ソートをアニメーションで可視化。ステップ実行や速度調整、カスタム入力、比較回数・書き込み回数のリアルタイム表示、疑似コード(pseudocode)表示に対応。すべてブラウザ内で完結します。

ツールを開く

選択ソート(Selection Sort)ビジュアライザー

ステップ実行、速度調整、カスタム入力、比較/交換回数のライブカウンター、擬似コードを備えたSelection Sortのアニメーション。すべてブラウザ内だけで完結します。

ツールを開く

マージソート ビジュアライザー

マージソートの動きをアニメーションで再現するツールです。ステップ実行、速度調整、独自のデータ入力、比較・書き込み回数のリアルタイム表示、擬似コード表示に対応。すべてブラウザ上だけで動作します。

ツールを開く

クイックソート・ビジュアライザー

ピボットとパーティションをハイライト表示しながらクイックソートをアニメーション表示。ステップ実行、速度調整、カスタム入力、リアルタイムのカウンター、擬似コードにも対応。ブラウザ上ですぐに動かせます。

ツールを開く

ヒープソート ビジュアライザー

ヒープソートのアルゴリズムをアニメーションで可視化。ステップ実行や速度調整、入力データのカスタマイズ、比較・交換回数のライブカウント、擬似コード表示に対応。ブラウザだけでその場に動かせます。

ツールを開く

Prefer a focused tool?

よくある質問

ヒープソートとは何ですか?

ヒープソートは、まず配列からmaxヒープを構築し、その後は根(最大値)を末尾と交換してsift-downでヒープの性質を復元する、という処理を繰り返すアルゴリズムです。これにより、配列の右側から徐々にソート済み領域が広がっていきます。

ヒープソートの時間計算量はどれくらいですか?

最良・平均・最悪のいずれの場合もO(n log n)です。ヒープの構築にO(n)かかり、n回の取り出し処理それぞれにO(log n)かかります。

ヒープソートは安定ソートですか?

いいえ、安定ソートではありません。ヒープ操作では離れた位置にある要素同士を交換するため、同じ値を持つ要素の相対的な順序が入れ替わることがあります。

ヒープソートとクイックソートはどう違いますか?

ヒープソートは最悪の場合でもO(n log n)を保証し、追加メモリもO(1)しか使いません。一方クイックソートは、キャッシュ局所性の良さや係数の小ささから、実際にはより高速に動作することが多いです。