{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/cpp-sorting-review/",
  "atlas": "https://www.pystone.net/?node=cpp-sorting-review#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/cpp-sorting-review.md",
  "context": "https://www.pystone.net/notes/cpp-sorting-review.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:cpp-sorting-review",
  "slug": "cpp-sorting-review",
  "title": "排序复习",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "排序复习",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "ea4977633697ad0875043177af0359a8764d73e2b5943a90bf7e8a79409a53a0",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "排序复习",
      "anchor": "排序复习",
      "citation": "https://www.pystone.net/notes/cpp-sorting-review/#%E6%8E%92%E5%BA%8F%E5%A4%8D%E4%B9%A0"
    },
    {
      "depth": 3,
      "text": "C++STL Sort实现",
      "anchor": "cstl-sort实现",
      "citation": "https://www.pystone.net/notes/cpp-sorting-review/#cstl-sort%E5%AE%9E%E7%8E%B0"
    },
    {
      "depth": 3,
      "text": "stablesort",
      "anchor": "stablesort",
      "citation": "https://www.pystone.net/notes/cpp-sorting-review/#stablesort"
    },
    {
      "depth": 3,
      "text": "快速排序",
      "anchor": "快速排序",
      "citation": "https://www.pystone.net/notes/cpp-sorting-review/#%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:programming-languages-and-runtimes",
      "title": "编程语言与运行时",
      "url": "https://www.pystone.net/notes/programming-languages-and-runtimes/",
      "atlas": "https://www.pystone.net/?node=programming-languages-and-runtimes#knowledge-atlas",
      "label": "编程语言与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "C++中的“排序复习”导航项",
      "citation": "https://www.pystone.net/notes/programming-languages-and-runtimes/#c"
    }
  ],
  "contentMarkdown": "# 排序复习\n\n﻿# 排序复习\n\n> 创建时间：2020/2/5 0:29\n\n### C++STL Sort实现\n\n  1. 数据量大时采用QuickSort快排算法，分段归并排序。\n  2. 一旦分段后的数据量小于某个门槛（16），为避免QuickSort快排的递归调用带来过大的额外负荷，就改用Insertion Sort插入排序。\n  3. 如果递归层次过深，还会改用HeapSort堆排序。\n\nhttps://blog.csdn.net/qq_35440678/article/details/80147601\n\n### stable_sort\n\n会对一段元素进行排序并保证 **维持相等元素的原始顺序**\nsort是快速排序实现，因此是不稳定的；\nstable_sort是归并排序实现，因此是稳定的。\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n>   * sort是快速排序实现，因此是不稳定的；stable_sort是归并排序实现，因此是稳定的。\n>   * 如果提供了比较函数，sort不要求比较函数的参数被限定为const，而stable_sort则要求参数被限定为const，否则编译不能通过。\n>\n\n### 快速排序\n\n给基准数据找其正确索引位置的过程\n\n  1. 先从数列中取出一个数作为基准数\n  2. 分区过程，将比这个数大的数全放到它的右边，小于或等于它的数全放到它的左边\n  3. 再对左右区间重复第二步，直到各区间只有一个数\n\n挖坑填数过程：\n\n  1. i =L; j = R; 将基准数挖出形成第一个坑a[i]。\n  2. j–由后向前找比它小的数，找到后挖出此数填前一个坑a[i]中。\n  3. i++由前向后找比它大的数，找到后也挖出此数填到前一个坑a[j]中。\n  4. 再重复执行2，3二步，直到i==j，将基准数填入a[i]中。\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n"
}
