跳至主要內容
產品桌面工具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壓得更快,解得更安心TASTEWINM以鍵盤為主、用兩個窗格整理檔案的檔案管理器

開發工具

TASTECODE在一個視窗中完成 Rust 應用程式的編輯、建置、偵錯與測試TASTESTUDIO從編輯到遠端測試,再到經核准的 AI 修正TASTEETL邊看數值,邊搬資料

伺服器

TASTEREMOTE在自己的伺服器上運行的遠端桌面TASTEGEO以 Rust 重新打造相容 GeoServer 的空間資訊伺服器TASTEMAIL把郵件的主導權拿回自己手上

投資

TASTESTOCK何時、做什麼——依你訂下的規則運作的股票自動交易

開始使用

下載所有產品的最新版本文件各產品使用方法購買訂閱與永久授權所有產品一覽全部產品
所有產品
市集資訊
技術

資訊

今日開發資訊語言、框架、GitHub 與 AI 官方發布開發文件語言、框架與演算法指南

程式語言

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

框架

Web 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular
伺服器框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express
應用程式與桌面框架TauriFlutterReact NativeElectron

演算法

演算法基礎複雜度分析 · 排序 · 搜尋 · 基礎資料結構 · 樹與堆積核心演算法圖形走訪 · 最短路徑與最小生成樹 · 動態規劃 · 貪婪演算法 · 分治法 · 回溯法進階演算法字串演算法 · 數學與數論 · 位元運算 · 線段樹 · 併查集實務演算法程式面試解題策略 · LRU 快取 · 速率限制 · 一致性雜湊
查看全部開發文件
社群
專案探索開發者與作品開發日誌決策與試錯提示詞可重複使用的指令AI 工具問題Claude、Codex、Gemini 的問題與替代做法作品展示已完成的成果社群指南我們如何公開打造
合作
技術傳授用 Rust 與 egui 打造原生應用程式網站建置10 種語言、帳號、文件與下載桌面應用程式建置Rust 與 egui,不用 WebView發布與付款流程簽章、MSI、deb、rpm、AppImage、雜湊銷售合作夥伴推薦即得 30%聯絡我們開發、合作與投資諮詢
服務
遠端桌面用瀏覽器連線你的電腦安裝包客製只裝需要的,設定預先放好
購買
繁
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
登入
登入
TASTEDEV

從小工具到更好的開發。

support@tastedev.net支持 TASTEDEV
產品產品系列購買文件下載市集客戶支援
社群專案開發日誌提示詞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 則留言

熱門留言由新到舊

登入 · 登入後即可留言。