本文へスキップ
製品デスクトップツール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
ディスカッション

ソート ガイド · 6/6

Real-World Use

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

You will rarely implement a sorting algorithm at work. What matters is knowing which algorithm your language uses, whether it is stable, and how to pass comparators safely. This chapter covers the built-in sorts of major languages, common pitfalls, and tools that can replace a full sort.

Built-in sorts by language

Language and functionAlgorithmStable
Python sorted, list.sortTimsortyes
Java Arrays.sort (primitive arrays)Dual-Pivot Quicksortnot applicable
Java Arrays.sort (object arrays), List.sortTimSortyes
C++ std::sortimplementation-defined, usually introsort-basedno
C++ std::stable_sortmerge sortyes
JavaScript Array.prototype.sortnot specified (V8 uses TimSort)yes (required since ES2019)
Go sort.Sort, slices.Sortpdqsort (sort since Go 1.19)no

Python's Timsort finds runs that are already sorted, extends short runs with binary insertion sort, and then merges runs. When one side keeps winning, galloping mode skips ahead with exponential search. That makes it very fast on sorted or nearly sorted data and on two sorted lists concatenated together. CPython 3.11 replaced the rule that decides the merge order with the Powersort policy, but stability and the O(n log n) worst case are unchanged. Java's object sort is a port of Python's Timsort.

The C++ standard does not prescribe an algorithm for std::sort; since C++11 it only requires O(n log n) comparisons. Major implementations such as libstdc++ use introsort: start with quicksort, switch to heapsort when recursion gets deeper than about 2 log n, and finish small ranges with insertion sort. When you need stability, use std::stable_sort.

When you do not need a full sort

If you only need part of the order, cheaper tools exist.

import heapq

scores = [72, 95, 88, 61, 99, 85, 90, 77]
print(heapq.nlargest(3, scores))   # [99, 95, 90]  O(n log k)
print(heapq.nsmallest(2, scores))  # [61, 72]

import bisect
ranked = sorted(scores)
bisect.insort(ranked, 93)          # insert while keeping the list sorted
print(ranked[bisect.bisect_left(ranked, 90):])  # 90 and above: [90, 93, 95, 99]
この章について質問がありますか?
前の章Practice Problems

In C++, std::partial_sort sorts only the first k elements, and std::nth_element places the k-th element correctly and partitions around it in average O(n), which is ideal for medians and percentiles.

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v{72, 95, 88, 61, 99, 85, 90, 77};
    std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end());
    std::cout << "median-ish: " << v[v.size() / 2] << '\n';  // 88
    std::partial_sort(v.begin(), v.begin() + 3, v.end(), std::greater<int>());
    std::cout << v[0] << ' ' << v[1] << ' ' << v[2] << '\n';  // 99 95 90
}

Databases avoid sorting for ORDER BY when an index already provides the order, and fall back to external merge sort, sorting chunks to disk and merging them, when the result does not fit in memory. The Unix sort command handles huge log files the same way.

Common pitfalls

  • JavaScript's default sort compares elements as strings: [10, 9, 1].sort() gives [1, 10, 9]. Always pass (a, b) => a - b for numbers.
  • An inconsistent comparator (not transitive, or random) produces garbage, and in Java can throw IllegalArgumentException: Comparison method violates its general contract!.
  • In Java, (a, b) -> a - b overflows for large values. Use Integer.compare or Comparator.comparingInt.
  • NaN compares false against everything and corrupts the result. Filter it out or handle it in the key.
  • Human-friendly string order is not code point order. For case, accents and language rules, use locale-aware comparison such as Intl.Collator or ICU.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Ranking {
    record Player(String name, int score) {}

    public static void main(String[] args) {
        List<Player> players = new ArrayList<>(List.of(
            new Player("lee", 90), new Player("kim", 95), new Player("park", 90)));
        players.sort(Comparator.comparingInt(Player::score).reversed()
                .thenComparing(Player::name));
        System.out.println(players);  // kim 95, lee 90, park 90
    }
}
const nums = [10, 9, 1];
console.log([...nums].sort());                // [1, 10, 9] string comparison
console.log([...nums].sort((a, b) => a - b)); // [1, 9, 10]

const names = ["Émile", "zoe", "Adam"];
console.log([...names].sort(new Intl.Collator("fr").compare)); // ["Adam", "Émile", "zoe"]

Further reading

  • Sorting Techniques (Python docs)
  • CPython listsort.txt: Timsort design notes
  • std::sort (cppreference)
  • Arrays (Java SE 21 API)
  • Array.prototype.sort() (MDN)
  • VisuAlgo: sorting visualizations

Summary

  • Knowing your built-in sort's algorithm and stability covers most practical needs.
  • For top-k, medians and keeping data sorted, heaps, selection and binary search beat a full sort.
  • Comparator consistency, numeric versus string comparison, NaN and locale rules cause most sorting bugs.

コメント 0件

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

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