<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF%2F01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95</id>
	<title>05-算法模板/01-排序算法 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF%2F01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95"/>
	<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;action=history"/>
	<updated>2026-05-20T22:20:05Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=345&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=345&amp;oldid=prev"/>
		<updated>2026-05-20T18:12:10Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 18:12的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-344:rev-345 --&gt;
&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=344&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=344&amp;oldid=prev"/>
		<updated>2026-05-20T16:50:33Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:50的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-245:rev-344 --&gt;
&lt;/table&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=245&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=245&amp;oldid=prev"/>
		<updated>2026-05-20T16:25:32Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:25的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-244:rev-245 --&gt;
&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=244&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=05-%E7%AE%97%E6%B3%95%E6%A8%A1%E6%9D%BF/01-%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;diff=244&amp;oldid=prev"/>
		<updated>2026-05-20T16:22:29Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== 选择排序 ==&lt;br /&gt;
&lt;br /&gt;
每次选出最小的放到前面。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;bits/stdc++.h&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int n, a[1005];&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    cin &amp;gt;&amp;gt; n;&lt;br /&gt;
    for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
        cin &amp;gt;&amp;gt; a[i];&lt;br /&gt;
    for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
        for (int j = i + 1; j &amp;lt;= n; j++)&lt;br /&gt;
            if (a[j] &amp;lt; a[i])&lt;br /&gt;
                swap(a[j], a[i]);&lt;br /&gt;
    for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
        cout &amp;lt;&amp;lt; a[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 冒泡排序 ==&lt;br /&gt;
&lt;br /&gt;
相邻两个比较，大的往后沉。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 1; i &amp;lt;= n - 1; i++)&lt;br /&gt;
    for (int j = 1; j &amp;lt;= n - i; j++)&lt;br /&gt;
        if (a[j] &amp;gt; a[j + 1])&lt;br /&gt;
            swap(a[j], a[j + 1]);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
优化：如果一轮没有交换，说明已经有序。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 1; i &amp;lt;= n - 1; i++)&lt;br /&gt;
{&lt;br /&gt;
    bool flag = false;&lt;br /&gt;
    for (int j = 1; j &amp;lt;= n - i; j++)&lt;br /&gt;
        if (a[j] &amp;gt; a[j + 1])&lt;br /&gt;
        {&lt;br /&gt;
            swap(a[j], a[j + 1]);&lt;br /&gt;
            flag = true;&lt;br /&gt;
        }&lt;br /&gt;
    if (!flag)&lt;br /&gt;
        break;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 插入排序 ==&lt;br /&gt;
&lt;br /&gt;
每次将一个新元素插入到已排序的部分。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 2; i &amp;lt;= n; i++)&lt;br /&gt;
    for (int j = i; j &amp;gt;= 2; j--)&lt;br /&gt;
        if (a[j] &amp;lt; a[j - 1])&lt;br /&gt;
            swap(a[j], a[j - 1]);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 计数排序 ==&lt;br /&gt;
&lt;br /&gt;
适用于值域较小的整数排序。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int cnt[2005] = {0};&lt;br /&gt;
for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
    cnt[a[i]]++;&lt;br /&gt;
for (int i = 1; i &amp;lt;= 2000; i++)&lt;br /&gt;
    for (int j = 1; j &amp;lt;= cnt[i]; j++)&lt;br /&gt;
        cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 地精排序 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int pos = 1;&lt;br /&gt;
while (pos != n)&lt;br /&gt;
{&lt;br /&gt;
    if (a[pos] &amp;lt;= a[pos + 1])&lt;br /&gt;
        pos++;&lt;br /&gt;
    else&lt;br /&gt;
    {&lt;br /&gt;
        swap(a[pos], a[pos + 1]);&lt;br /&gt;
        if (pos != 1)&lt;br /&gt;
            pos--;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== STL &amp;lt;code&amp;gt;sort&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
sort(a + 1, a + n + 1);                   // 升序 [1, n]&lt;br /&gt;
sort(a + 1, a + n + 1, greater&amp;lt;int&amp;gt;());    // 降序&lt;br /&gt;
sort(a + 1, a + n + 1, cmp);               // 自定义比较函数&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 自定义比较函数 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
bool cmp(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
    return a &amp;gt; b; // 降序&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 对结构体排序 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct Student { int score, id; };&lt;br /&gt;
&lt;br /&gt;
bool cmp(const Student &amp;amp;a, const Student &amp;amp;b)&lt;br /&gt;
{&lt;br /&gt;
    if (a.score != b.score)&lt;br /&gt;
        return a.score &amp;gt; b.score; // 分数高在前&lt;br /&gt;
    return a.id &amp;lt; b.id;           // 分数相同，id 小在前&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 时间复杂度 ===&lt;br /&gt;
&lt;br /&gt;
- STL &amp;lt;code&amp;gt;sort&amp;lt;/code&amp;gt;：&amp;lt;math&amp;gt;O(n\log n)&amp;lt;/math&amp;gt;&lt;br /&gt;
- 选择/冒泡/插入：&amp;lt;math&amp;gt;O(n^2)&amp;lt;/math&amp;gt;&lt;br /&gt;
- 计数排序：&amp;lt;math&amp;gt;O(n + \text{值域})&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:算法模板]]&lt;br /&gt;
[[Category:三三文档]]&lt;/div&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
</feed>