-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb78b.html
796 lines (747 loc) · 106 KB
/
b78b.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"><title>排序算法详解 | 伴随的小站</title><meta name="keywords" content="软件设计师,软考,排序算法"><meta name="author" content="伴随"><meta name="copyright" content="伴随"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#18171d"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-touch-fullscreen" content="yes"><meta name="apple-mobile-web-app-title" content="排序算法详解"><meta name="application-name" content="排序算法详解"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="#18171d"><meta property="og:type" content="article"><meta property="og:title" content="排序算法详解"><meta property="og:url" content="https://ctext.top/b78b.html"><meta property="og:site_name" content="伴随的小站"><meta property="og:description" content="小知识 如何判断一个排序算法是否稳定 稳定:如果a原本在b前面,而a&#x3D;b,排序之后a仍然在b的前面; 不稳定:如果a原本在b的前面,而a&#x3D;b,排序之后a可能会出现在b的后面; 速记 不稳定算法:快选堆希 排序方式 In-place(原地算法):指算法执行过程中不需要额外的辅助空间,而是在原"><meta property="og:locale" content="zh-CN"><meta property="og:image" content="https://up.ctext.top/article/2024/04/sort.png"><meta property="article:author" content="伴随"><meta property="article:tag"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://up.ctext.top/article/2024/04/sort.png"><meta name="description" content="小知识 如何判断一个排序算法是否稳定 稳定:如果a原本在b前面,而a&#x3D;b,排序之后a仍然在b的前面; 不稳定:如果a原本在b的前面,而a&#x3D;b,排序之后a可能会出现在b的后面; 速记 不稳定算法:快选堆希 排序方式 In-place(原地算法):指算法执行过程中不需要额外的辅助空间,而是在原"><link rel="shortcut icon" href="https://up.ctext.top/favicon.svg"><link rel="canonical" href="https://ctext.top/b78b.html"><link rel="preconnect" href="//cdn.cbd.int"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="xxx"/><meta name="baidu-site-verification" content="codeva-ulCeD8b6u5"/><meta name="msvalidate.01" content="xxx"/><meta name="sogou_site_verification" content="YdTw3LB3JQ"/><meta name="360-site-verification" content="5f481d0e3172c0894747aba683bb1191"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.cbd.int/@fortawesome/fontawesome-free@6.4.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/node-snackbar@0.1.16/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/@fancyapps/ui@5.0.28/dist/fancybox/fancybox.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/anzhiyu-theme-static@1.0.0/swiper/swiper.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?643616874a8f4f5ed38e6146dfd84820";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = {
linkPageTop: undefined,
peoplecanvas: undefined,
postHeadAiDescription: {"enable":true,"gptName":null,"mode":"tianli","switchBtn":false,"btnLink":null,"randomNum":3,"basicWordCount":1000,"key":"XlJ1hsZTEze","Referer":"/"},
diytitle: {"enable":true,"leaveTitle":"分享使快乐加倍!(´▽`)","backTitle":"慢慢看~ヾ(•ω•`)o"},
LA51: undefined,
greetingBox: {"enable":true,"default":"晚上好👋","list":[{"greeting":"晚安啦🌙,做个好梦,期待明天的美好!😴💤","startTime":0,"endTime":6},{"greeting":"早上好🌞,新的一天,愿你充满活力和正能量!💪🌈","startTime":6,"endTime":10},{"greeting":"上午好👋,工作/学习加油,期待你的出色表现!🌟📚","startTime":10,"endTime":12},{"greeting":"午餐时间到🍽️,好好吃饭,下午继续加油!🍛💼","startTime":12,"endTime":14},{"greeting":"下午好👋,午后时光,放松心情,享受片刻宁静。☕️💆♀️","startTime":14,"endTime":17},{"greeting":"傍晚好,一天即将结束,为自己点个赞吧!👍🎉","startTime":17,"endTime":19},{"greeting":"晚餐时间啦🍲,享受美食,犒劳一下自己吧!🍽️🥳","startTime":19,"endTime":21},{"greeting":"晚上好🌃,夜晚的时光,放松身心,好好休息吧!🛌💤","startTime":21,"endTime":24}]},
twikooEnvId: '',
commentBarrageConfig:undefined,
music_page_default: "nav_music",
root: '/',
preloader: {"source":3},
friends_vue_info: undefined,
navMusic: true,
mainTone: {"mode":"api","api":"https://color.ctext.top/api?img=","cover_change":true},
authorStatus: {"skills":["🤖️ 数码科技爱好者","🔍 分享与热心帮助","🏠 智能家居小能手","🔨 设计开发一条龙"]},
algolia: undefined,
localSearch: undefined,
translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简","rightMenuMsgToTraditionalChinese":"转为繁体","rightMenuMsgToSimplifiedChinese":"转为简体"},
noticeOutdate: undefined,
highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":330},
copy: {
success: '复制成功',
error: '复制错误',
noSupport: '浏览器不支持'
},
relativeDate: {
homepage: false,
simplehomepage: true,
post: false
},
runtime: '天',
date_suffix: {
just: '刚刚',
min: '分钟前',
hour: '小时前',
day: '天前',
month: '个月前'
},
copyright: {"copy":true,"copyrightEbable":false,"limitCount":50,"languages":{"author":"作者: 伴随","link":"链接: ","source":"来源: 伴随的小站","info":"著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。","copySuccess":"复制成功,复制和转载请标注本文地址"}},
lightbox: 'fancybox',
Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#425AEF","bgDark":"#1f1f1f","position":"top-center"},
source: {
justifiedGallery: {
js: 'https://cdn.cbd.int/flickr-justified-gallery@2.1.2/dist/fjGallery.min.js',
css: 'https://cdn.cbd.int/flickr-justified-gallery@2.1.2/dist/fjGallery.css'
}
},
isPhotoFigcaption: false,
islazyload: true,
isAnchor: false,
shortcutKey: undefined,
autoDarkmode: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
configTitle: '伴随的小站',
title: '排序算法详解',
postAI: 'true',
pageFillDescription: '小知识, 如何判断一个排序算法是否稳定, 速记, 排序方式, 算法策略, 算法详解, 总结小知识如何判断一个排序算法是否稳定稳定如果原本在前面而排序之后仍然在的前面不稳定如果原本在的前面而排序之后可能会出现在的后面速记不稳定算法快选堆希排序方式原地算法指算法执行过程中不需要额外的辅助空间而是在原始数据上进行操作非原地算法指算法执行过程中需要额外的辅助空间来存储数据而不会在原始数据上进行直接操作所以非原地算法通常具有较高的空间复杂度算法策略分治法快速排序归并排序算法详解冒泡排序选择排序插入排序希尔排序归并排序快速排序堆排序计数排序桶排序基数排序冒泡排序也是一种简单直观的排序算法它重复地走访过要排序的数列一次比较两个元素如果他们的顺序错误就把他们交换过来走访数列的工作是重复地进行直到没有再需要交换也就是说该数列已经排序完成基本步骤比较相邻的两个元素如果前一个比后一个大升序排序或者小降序排序则交换它们的位置对每一对相邻元素重复步骤直到列表末尾这样一次遍历完成后最大或最小的元素将移动到列表末尾针对剩下的未排序元素重复以上步骤直到列表中的所有元素都排好序选择排序是一种简单直观的排序算法无论什么数据进去都是的时间复杂度所以用到它的时候数据规模越小越好唯一的好处可能就是不占用额外的内存空间基本步骤遍历数组从未排序部分找到最小或最大的元素交换位置将找到的最小或最大元素与未排序部分的第一个元素交换位置缩小范围将未排序部分的范围缩小一个元素即将排序范围向右移动一个位置重复步骤至步骤直到所有元素都被排序完成插入排序的代码实现虽然没有冒泡排序和选择排序那么简单粗暴但它的原理应该是最容易理解的了因为只要打过扑克牌的人都应该能够秒懂插入排序是一种最简单直观的排序算法它的工作原理是通过构建有序序列对于未排序数据在已排序序列中从后向前扫描找到相应位置并插入基本步骤初始状态将第一个元素视为已排序部分其余元素视为未排序部分遍历未排序部分依次取出未排序部分的元素插入已排序部分将取出的元素与已排序部分的元素依次比较找到合适的位置插入重复步骤至步骤直到所有元素都被插入到已排序部分希尔排序也称递减增量排序算法是插入排序的一种更高效的改进版本但希尔排序是非稳定排序算法基本步骤确定间隔序列选择一个增量序列也称为间隔序列该序列的最后一个元素必须为常用的增量序列有希尔建议的序列例如或者序列分组排序根据增量序列将原始列表分割成若干个子列表对每个子列表分别进行插入排序逐步缩小间隔逐渐缩小增量重复步骤直到增量为最后的插入排序当增量为时进行最后一次插入排序此时列表已经被预排序插入排序的效率较高归并排序是建立在归并操作上的一种有效的排序算法该算法是采用分治法的一个非常典型的应用基本步骤分割将待排序的列表分成两个子列表直到每个子列表的长度为即列表无法再分割排序对每个子列表进行递归排序递归的过程就是不断地将列表分割并排序合并将两个已排序的子列表合并成一个有序列表合并过程中比较两个子列表的头部元素将较小或较大的元素放入合并后的列表中直到其中一个子列表为空然后将另一个子列表的剩余部分直接放入合并后的列表中快速排序是由东尼霍尔所发展的一种排序算法在平均状况下排序个项目要次比较在最坏状况下则需要次比较但这种状况并不常见事实上快速排序通常明显比其他算法更快因为它的内部循环可以在大部分的架构上很有效率地被实现出来基本步骤选择基准从列表中选择一个元素作为基准元素一般情况下是选择数组的第一个或最后一个分区将列表中小于基准元素的元素放在基准元素的左边大于基准元素的元素放在右边基准元素放在中间这个过程称为分区操作递归排序对左右两个子列表分别进行递归排序直到列表为空或只包含一个元素合并由于快速排序是原地排序算法不需要显式的合并步骤再详细一点原数组选择基准元素分成左半部分和右半部分对左半部分递归调用快速排序算法得到有序数组对右半部分选择基准元素分成左半部分和右半部分对左半部分递归调用快速排序算法得到有序数组对右半部分选择基准元素分成左半部分和右半部分对左半部分选择基准元素分成左半部分和右半部分对左半部分递归调用快速排序算法得到有序数组对右半部分递归调用快速排序算法得到有序数组将左半部分基准元素和右半部分拼接起来得到有序数组将左半部分基准元素和右半部分拼接起来得到有序数组将左半部分基准元素和右半部分拼接起来得到有序数组堆排序是指利用堆这种数据结构所设计的一种排序算法堆积是一个近似完全二叉树的结构并同时满足堆积的性质即子结点的键值或索引总是小于或者大于它的父节点堆排序可以说是一种利用堆的概念来排序的选择排序分为两种方法大顶堆每个节点的值都大于或等于其子节点的值在堆排序算法中用于升序排列小顶堆每个节点的值都小于或等于其子节点的值在堆排序算法中用于降序排列基本步骤建立堆将待排序的列表构建成一个最大堆这一步通常是从列表的中间位置开始自底向上地调整每个节点使得整个列表满足堆的性质排序将最大堆中的根节点即最大值与堆中的最后一个元素交换位置并将堆的大小减交换后可能会破坏堆的性质因此需要进行堆调整操作使得堆重新满足堆的性质重复步骤直到堆的大小为此时整个列表已经排序完成计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中作为一种线性时间复杂度的排序计数排序要求输入的数据必须是有确定范围的整数基本步骤统计元素出现次数遍历待排序的列表统计每个元素出现的次数并将统计结果存储在一个额外的计数数组中计数数组的索引对应于待排序元素的值而数组中的值对应于该元素出现的次数累加计数对计数数组进行累加操作使得每个索引位置的值等于其前面所有索引位置的值之和这一步可以使得计数数组中的值表示小于等于该索引值的元素个数输出排序结果根据计数数组中的统计结果将待排序列表中的元素放置到正确的位置上最终得到排序结果桶排序是计数排序的升级版它利用了函数的映射关系高效与否的关键就在于这个映射函数的确定为了使桶排序更加高效我们需要做到这两点在额外空间充足的情况下尽量增大桶的数量使用的映射函数能够将输入的个数据均匀的分配到个桶中基本步骤划分桶根据待排序元素的范围确定一定数量的桶并将待排序元素分配到相应的桶中桶内排序对每个桶中的元素进行排序可以使用任何适合的排序算法如插入排序快速排序等合并结果按照桶的顺序依次取出各个桶中的元素即可得到有序的结果基数排序是一种非比较型整数排序算法其原理是将整数按位数切割成不同的数字然后按每个位数分别比较由于整数也可以表达字符串比如名字或日期和特定格式的浮点数所以基数排序也不是只能使用于整数基本步骤按位排序从最低位开始对待排序的元素按照当前位数进行排序可以使用稳定的排序算法如计数排序或桶排序重复排序重复第一步的过程对每一位进行排序直到对最高位进行排序合并结果完成所有位的排序后即可得到有序的结果总结排序算法平均时间复杂度最好情况最坏情况空间复杂度排序方式稳定性冒泡排序稳定选择排序不稳定插入排序稳定希尔排序不稳定归并排序稳定快速排序不稳定堆排序不稳定计数排序稳定桶排序稳定基数排序稳定',
isPost: true,
isHome: false,
isHighlightShrink: false,
isToc: true,
postUpdate: '2024-04-20 18:07:20',
postMainColor: '#cf9894',
}</script><noscript><style type="text/css">
#nav {
opacity: 1
}
.justified-gallery img {
opacity: 1
}
#recent-posts time,
#post-meta time {
display: inline !important
}
</style></noscript><script>(win=>{
win.saveToLocal = {
set: (key, value, ttl) => {
if (ttl === 0) return
const now = Date.now()
const expiry = now + ttl * 86400000
const item = {
value,
expiry
}
localStorage.setItem(key, JSON.stringify(item))
},
get: key => {
const itemStr = localStorage.getItem(key)
if (!itemStr) {
return undefined
}
const item = JSON.parse(itemStr)
const now = Date.now()
if (now > item.expiry) {
localStorage.removeItem(key)
return undefined
}
return item.value
}
}
win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
const script = document.createElement('script')
script.src = url
script.async = true
script.onerror = reject
script.onload = script.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null
resolve()
}
Object.keys(attr).forEach(key => {
script.setAttribute(key, attr[key])
})
document.head.appendChild(script)
})
win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = url
if (id) link.id = id
link.onerror = reject
link.onload = link.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
link.onload = link.onreadystatechange = null
resolve()
}
document.head.appendChild(link)
})
win.activateDarkMode = () => {
document.documentElement.setAttribute('data-theme', 'dark')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#18171d')
}
}
win.activateLightMode = () => {
document.documentElement.setAttribute('data-theme', 'light')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#f7f9fe')
}
}
const t = saveToLocal.get('theme')
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
if (t === undefined) {
if (isLightMode) activateLightMode()
else if (isDarkMode) activateDarkMode()
else if (isNotSpecified || hasNoSupport) {
const now = new Date()
const hour = now.getHours()
const isNight = hour <= 6 || hour >= 18
isNight ? activateDarkMode() : activateLightMode()
}
window.matchMedia('(prefers-color-scheme: dark)').addListener(e => {
if (saveToLocal.get('theme') === undefined) {
e.matches ? activateDarkMode() : activateLightMode()
}
})
} else if (t === 'light') activateLightMode()
else activateDarkMode()
const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
if (asideStatus === 'hide') {
document.documentElement.classList.add('hide-aside')
} else {
document.documentElement.classList.remove('hide-aside')
}
}
const detectApple = () => {
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
document.documentElement.classList.add('apple')
}
}
detectApple()
})(window)</script><link rel="stylesheet" href="/css/font.css"><link rel="stylesheet" href="/css/welcome.css"><link rel="stylesheet" href="/css/imgloaded.css"><link rel="stylesheet" href="/css/hideRolling.css"><link rel="stylesheet" href="/css/countdown.css"><link rel="stylesheet" href="/css/essay-animation.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 7.3.0"></head><body data-type="anzhiyu"><div id="web_bg"></div><div id="an_music_bg"></div><div id="loading-box" onclick="document.getElementById("loading-box").classList.add("loaded")"><div class="loading-bg"><img class="loading-img nolazyload" alt="加载头像" src="https://up.ctext.top/avatar.png"/><div class="loading-image-dot"></div></div></div><script>const preloader = {
endLoading: () => {
document.getElementById('loading-box').classList.add("loaded");
},
initLoading: () => {
document.getElementById('loading-box').classList.remove("loaded")
}
}
window.addEventListener('load',()=> { preloader.endLoading() })
setTimeout(function(){preloader.endLoading();},10000)
if (true) {
document.addEventListener('pjax:send', () => { preloader.initLoading() })
document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><link rel="stylesheet" href="https://cdn.cbd.int/anzhiyu-theme-static@1.1.10/progress_bar/progress_bar.css"/><script async="async" src="https://cdn.cbd.int/pace-js@1.2.4/pace.min.js" data-pace-options="{ "restartOnRequestAfter":false,"eventLag":false}"></script><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><div id="nav-group"><span id="blog_name"><div class="back-home-button"><i class="anzhiyufont anzhiyu-icon-grip-vertical"></i><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">关于</div><div class="back-menu-list"><a class="back-menu-item" href="/about/" title="我"><img class="back-menu-item-icon" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/ico/orange.svg" alt="我"/><span class="back-menu-item-text">我</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">博客</div><div class="back-menu-list"><a class="back-menu-item" href="/album/" title="我的相册"><img class="back-menu-item-icon" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/ico/strawberry.svg" alt="我的相册"/><span class="back-menu-item-text">我的相册</span></a><a class="back-menu-item" href="/essay/" title="记录生活"><img class="back-menu-item-icon" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/ico/apple.svg" alt="记录生活"/><span class="back-menu-item-text">记录生活</span></a></div></div></div></div><a id="site-name" href="/" accesskey="h"><div class="title">伴随的小站</div><i class="anzhiyufont anzhiyu-icon-house-chimney"></i></a></span><div class="mask-name-container"><div id="name-container"><a id="page-name" href="javascript:anzhiyu.scrollToDest(0, 500)">PAGE_NAME</a></div></div><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 文章</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/"><i class="anzhiyufont anzhiyu-icon-grip-vertical faa-tada" style="font-size: 0.9em;"></i><span> 主页</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/archives/"><i class="anzhiyufont anzhiyu-icon-box-archive faa-tada" style="font-size: 0.9em;"></i><span> 总览</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><i class="anzhiyufont anzhiyu-icon-shapes faa-tada" style="font-size: 0.9em;"></i><span> 分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><i class="anzhiyufont anzhiyu-icon-tags faa-tada" style="font-size: 0.9em;"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 我的</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/growth.html"><i class="anzhiyufont anzhiyu-icon-heartbeat faa-tada" style="font-size: 0.9em;"></i><span> 博客岁月</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/music/"><i class="anzhiyufont anzhiyu-icon-music faa-tada" style="font-size: 0.9em;"></i><span> 音乐馆</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/album/"><i class="anzhiyufont anzhiyu-icon-images faa-tada" style="font-size: 0.9em;"></i><span> 相册集</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/essay/"><i class="anzhiyufont anzhiyu-icon-lightbulb faa-tada" style="font-size: 0.9em;"></i><span> 记录生活</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 小工具</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/air-conditioner/"><i class="anzhiyufont anzhiyu-icon-fan faa-tada" style="font-size: 0.9em;"></i><span> 小空调</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/gradation/"><i class="anzhiyufont anzhiyu-icon-envelope faa-tada" style="font-size: 0.9em;"></i><span> 渐变背景生成</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/9abe.html"><i class="anzhiyufont anzhiyu-icon-artstation faa-tada" style="font-size: 0.9em;"></i><span> tag-plugins文档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/nav"><i class="anzhiyufont anzhiyu-icon-link faa-tada" style="font-size: 0.9em;"></i><span> 网址导航</span></a></li><li><a class="site-page child faa-parent animated-hover" target="_blank" rel="noopener" href="https://map.ctext.top"><i class="anzhiyufont anzhiyu-icon-keyboard faa-tada" style="font-size: 0.9em;"></i><span> 飞车随机选图工具</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 关于</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/about/"><i class="anzhiyufont anzhiyu-icon-paper-plane faa-tada" style="font-size: 0.9em;"></i><span> 关于自己</span></a></li><li><a class="site-page child faa-parent animated-hover" href="javascript:toRandomPost()"><i class="anzhiyufont anzhiyu-icon-shoe-prints1 faa-tada" style="font-size: 0.9em;"></i><span> 随便逛逛</span></a></li></ul></div></div></div><div id="nav-right"><div class="nav-button" id="randomPost_button"><a class="site-page" onclick="toRandomPost()" title="随机前往一个文章" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-dice"></i></a></div><input id="center-console" type="checkbox"/><label class="widget" for="center-console" title="中控台" onclick="anzhiyu.switchConsole();"><i class="left"></i><i class="widget center"></i><i class="widget right"></i></label><div id="console"><div class="console-card-group-reward"><ul class="reward-all console-card"><li class="reward-item"><a href="https://up.ctext.top/pay/wx.png" target="_blank"><img class="post-qr-code-img" alt="微信" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/pay/wx.png"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://up.ctext.top/pay/zfb.png" target="_blank"><img class="post-qr-code-img" alt="支付宝" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/pay/zfb.png"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div><div class="console-card-group"><div class="console-card-group-left"><div class="console-card" id="card-newest-comments"><div class="card-content"><div class="author-content-item-tips">互动</div><span class="author-content-item-title"> 最新评论</span></div><div class="aside-list"><span>正在加载中...</span></div></div></div><div class="console-card-group-right"><div class="console-card tags"><div class="card-content"><div class="author-content-item-tips">音乐</div><span class="author-content-item-title">灵魂的碰撞💥</span></div></div><div class="console-card history"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-box-archiv"></i><span>文章</span></div><div class="card-archives"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-archive"></i><span>归档</span><a class="card-more-btn" href="/archives/" title="查看更多">
<i class="anzhiyufont anzhiyu-icon-angle-right"></i></a></div><ul class="card-archive-list"><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/11/"><span class="card-archive-list-date">十一月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">1</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/10/"><span class="card-archive-list-date">十月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">2</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/08/"><span class="card-archive-list-date">八月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">3</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/07/"><span class="card-archive-list-date">七月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">2</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/06/"><span class="card-archive-list-date">六月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">5</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/05/"><span class="card-archive-list-date">五月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">2</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/04/"><span class="card-archive-list-date">四月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">3</span><span>篇</span></div></a></li><li class="card-archive-list-item"><a class="card-archive-list-link" href="/archives/2024/03/"><span class="card-archive-list-date">三月 2024</span><div class="card-archive-list-count-group"><span class="card-archive-list-count">4</span><span>篇</span></div></a></li></ul></div><hr/></div></div></div><div class="button-group"><div class="console-btn-item"><a class="darkmode_switchbutton" title="显示模式切换" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-moon"></i></a></div><div class="console-btn-item" id="consoleHideAside" onclick="anzhiyu.hideAsideBtn()" title="边栏显示控制"><a class="asideSwitch"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></a></div><div class="console-btn-item" id="consoleMusic" onclick="anzhiyu.musicToggle()" title="音乐开关"><a class="music-switch"><i class="anzhiyufont anzhiyu-icon-music"></i></a></div></div><div class="console-mask" onclick="anzhiyu.hideConsole()" href="javascript:void(0);"></div></div><div class="nav-button" id="nav-totop"><a class="totopbtn" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i><span id="percent" onclick="anzhiyu.scrollToDest(0,500)">0</span></a></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);" title="切换"><i class="anzhiyufont anzhiyu-icon-bars"></i></a></div></div></div></nav><div id="post-info"><div id="post-firstinfo"><div class="meta-firstline"><a class="post-meta-original">原创</a><span class="post-meta-categories"><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-inbox post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E8%BD%AF%E8%80%83%E7%AC%94%E8%AE%B0/" itemprop="url">软考笔记</a></span><span class="article-meta tags"><a class="article-meta__tags" href="/tags/%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E5%B8%88/" tabindex="-1" itemprop="url"> <span> <i class="anzhiyufont anzhiyu-icon-hashtag"></i>软件设计师</span></a><a class="article-meta__tags" href="/tags/%E8%BD%AF%E8%80%83/" tabindex="-1" itemprop="url"> <span> <i class="anzhiyufont anzhiyu-icon-hashtag"></i>软考</span></a><a class="article-meta__tags" href="/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/" tabindex="-1" itemprop="url"> <span> <i class="anzhiyufont anzhiyu-icon-hashtag"></i>排序算法</span></a></span></div></div><h1 class="post-title" itemprop="name headline">排序算法详解</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="anzhiyufont anzhiyu-icon-calendar-days post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" itemprop="dateCreated datePublished" datetime="2024-04-06T10:07:20.000Z" title="发表于 2024-04-06 18:07:20">2024-04-06</time><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-history post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" itemprop="dateCreated datePublished" datetime="2024-04-20T10:07:20.000Z" title="更新于 2024-04-20 18:07:20">2024-04-20</time></span></div><div class="meta-secondline"><span class="post-meta-separator"></span><span class="post-meta-wordcount"><i class="anzhiyufont anzhiyu-icon-file-word post-meta-icon" title="文章字数"></i><span class="post-meta-label" title="文章字数">字数总计:</span><span class="word-count" title="文章字数">3.1k</span><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-clock post-meta-icon" title="阅读时长"></i><span class="post-meta-label" title="阅读时长">阅读时长:</span><span>9分钟</span></span><span class="post-meta-separator"></span><span class="post-meta-pv-cv" id="" data-flag-title="排序算法详解"><i class="anzhiyufont anzhiyu-icon-fw-eye post-meta-icon"></i><span class="post-meta-label" title="阅读量">阅读量:</span><span id="busuanzi_value_page_pv"><i class="anzhiyufont anzhiyu-icon-spinner anzhiyu-spin"></i></span></span><span class="post-meta-separator"> </span><span class="post-meta-position" title="作者IP属地为北京"><i class="anzhiyufont anzhiyu-icon-location-dot"></i>北京</span></div></div></div><section class="main-hero-waves-area waves-area"><svg class="waves-svg" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M -160 44 c 30 0 58 -18 88 -18 s 58 18 88 18 s 58 -18 88 -18 s 58 18 88 18 v 44 h -352 Z"></path></defs><g class="parallax"><use href="#gentle-wave" x="48" y="0"></use><use href="#gentle-wave" x="48" y="3"></use><use href="#gentle-wave" x="48" y="5"></use><use href="#gentle-wave" x="48" y="7"></use></g></svg></section><div id="post-top-cover"><img class="nolazyload" id="post-top-bg" src="https://up.ctext.top/article/2024/04/sort.png"></div></header><main id="blog-container"><div class="layout" id="content-inner"><div id="post"><div class="post-ai-description"><div class="ai-title"><i class="anzhiyufont anzhiyu-icon-bilibili"></i><div class="ai-title-text">AI-摘要(由百度千帆大模型提供生成摘要能力)</div><i class="anzhiyufont anzhiyu-icon-arrow-rotate-right"></i><i class="anzhiyufont anzhiyu-icon-circle-dot" title="朗读摘要"></i><div id="ai-tag">Tianli GPT</div></div><div class="ai-explanation">AI初始化中...</div><div class="ai-btn-box"><div class="ai-btn-item" id="ccnone">介绍自己 🙈</div><div class="ai-btn-item">生成本文简介 👋</div><div class="ai-btn-item">推荐相关文章 📖</div><div class="ai-btn-item">前往主页 🏠</div><div class="ai-btn-item" id="go-tianli-blog">前往爱发电购买</div></div><script data-pjax src="/js/anzhiyu/ai_abstract.js"></script></div><article class="post-content" id="article-container" itemscope itemtype="https://ctext.top/b78b.html"><header><a class="post-meta-categories" href="/categories/%E8%BD%AF%E8%80%83%E7%AC%94%E8%AE%B0/" itemprop="url">软考笔记</a><a href="/tags/%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E5%B8%88/" tabindex="-1" itemprop="url">软件设计师</a><a href="/tags/%E8%BD%AF%E8%80%83/" tabindex="-1" itemprop="url">软考</a><a href="/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/" tabindex="-1" itemprop="url">排序算法</a><h1 id="CrawlerTitle" itemprop="name headline">排序算法详解</h1><span itemprop="author" itemscope itemtype="http://schema.org/Person">伴随</span><time itemprop="dateCreated datePublished" datetime="2024-04-06T10:07:20.000Z" title="发表于 2024-04-06 18:07:20">2024-04-06</time><time itemprop="dateCreated datePublished" datetime="2024-04-20T10:07:20.000Z" title="更新于 2024-04-20 18:07:20">2024-04-20</time></header><h1>小知识</h1>
<h3 id="如何判断一个排序算法是否稳定">如何判断一个排序算法是否稳定</h3>
<ul>
<li><strong>稳定</strong>:如果a原本在b前面,而a=b,排序之后a仍然在b的前面;</li>
<li><strong>不稳定</strong>:如果a原本在b的前面,而a=b,排序之后a可能会出现在b的后面;</li>
</ul>
<h3 id="速记">速记</h3>
<ul>
<li><strong>不稳定算法</strong>:<wavy>快选堆希</wavy></li>
</ul>
<h3 id="排序方式">排序方式</h3>
<ul>
<li><strong>In-place(原地算法)</strong>:指算法执行过程中不需要额外的辅助空间,而是在原始数据上进行操作。</li>
<li><strong>Out-of-place(非原地算法)</strong>:指算法执行过程中需要额外的辅助空间来存储数据,而不会在原始数据上进行直接操作。</li>
</ul>
<mark class="hl-label purple">所以非原地算法通常具有较高的空间复杂度</mark>
<h3 id="算法策略">算法策略</h3>
<ul>
<li><strong>分治法</strong>:快速排序,归并排序</li>
</ul>
<h1>算法详解</h1>
<div class="tabs" id="test1"><ul class="nav-tabs"><button type="button" class="tab active" data-href="test1-1">冒泡排序</button><button type="button" class="tab " data-href="test1-2">选择排序</button><button type="button" class="tab " data-href="test1-3">插入排序</button><button type="button" class="tab " data-href="test1-4">希尔排序</button><button type="button" class="tab " data-href="test1-5">归并排序</button><button type="button" class="tab " data-href="test1-6">快速排序</button><button type="button" class="tab " data-href="test1-7">堆排序</button><button type="button" class="tab " data-href="test1-8">计数排序</button><button type="button" class="tab " data-href="test1-9">桶排序</button><button type="button" class="tab " data-href="test1-10">基数排序</button></ul><div class="tab-contents"><div class="tab-item-content active" id="test1-1"><div class="note info no-icon flat"><p> 冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li>
<p>比较相邻的两个元素,如果前一个比后一个大(升序排序)或者小(降序排序),则交换它们的位置。</p>
</li>
<li>
<p>对每一对相邻元素重复步骤1,直到列表末尾。这样一次遍历完成后,最大(或最小)的元素将移动到列表末尾。</p>
</li>
<li>
<p>针对剩下的未排序元素重复以上步骤,直到列表中的所有元素都排好序。</p>
</li>
</ol>
<img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://www.runoob.com/wp-content/uploads/2019/03/bubbleSort.gif"/></div><div class="tab-item-content" id="test1-2"><div class="note info no-icon flat"><p> 选择排序是一种简单直观的排序算法,无论什么数据进去都是 O(n²) 的时间复杂度。所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>遍历数组</strong>:从未排序部分找到最小(或最大)的元素。</li>
<li><strong>交换位置</strong>:将找到的最小(或最大)元素与未排序部分的第一个元素交换位置。</li>
<li><strong>缩小范围</strong>:将未排序部分的范围缩小一个元素,即将排序范围向右移动一个位置。</li>
<li><strong>重复步骤1至步骤3</strong>,直到所有元素都被排序完成。</li>
</ol>
<img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/selectionSort.gif"></div><div class="tab-item-content" id="test1-3"><div class="note info no-icon flat"><p> 插入排序的代码实现虽然没有冒泡排序和选择排序那么简单粗暴,但它的原理应该是最容易理解的了,因为只要打过扑克牌的人都应该能够秒懂。插入排序是一种最简单直观的排序算法,它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>初始状态</strong>:将第一个元素视为已排序部分,其余元素视为未排序部分。</li>
<li><strong>遍历未排序部分</strong>:依次取出未排序部分的元素。</li>
<li><strong>插入已排序部分</strong>:将取出的元素与已排序部分的元素依次比较,找到合适的位置插入。</li>
<li><strong>重复步骤2至步骤3</strong>,直到所有元素都被插入到已排序部分。</li>
</ol>
<img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/insertionSort.gif"></div><div class="tab-item-content" id="test1-4"><div class="note info no-icon flat"><p> 希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本。但希尔排序是非稳定排序算法。</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>确定间隔序列</strong>:选择一个增量序列(也称为间隔序列),该序列的最后一个元素必须为1。常用的增量序列有希尔建议的序列(例如:{n/2, n/4, …, 1})或者Sedgewick序列。</li>
<li><strong>分组排序</strong>:根据增量序列,将原始列表分割成若干个子列表,对每个子列表分别进行插入排序。</li>
<li><strong>逐步缩小间隔</strong>:逐渐缩小增量,重复步骤2,直到增量为1。</li>
<li><strong>最后的插入排序</strong>:当增量为1时,进行最后一次插入排序,此时列表已经被“预排序”,插入排序的效率较高。</li>
</ol>
<img decoding="async" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/Sorting_shellsort_anim.gif" alt=""></div><div class="tab-item-content" id="test1-5"><div class="note info no-icon flat"><p> 归并排序(Merge sort)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>分割</strong>:将待排序的列表分成两个子列表,直到每个子列表的长度为1(即列表无法再分割)。</li>
<li><strong>排序</strong>:对每个子列表进行递归排序。递归的过程就是不断地将列表分割并排序。</li>
<li><strong>合并</strong>:将两个已排序的子列表合并成一个有序列表。合并过程中,比较两个子列表的头部元素,将较小(或较大)的元素放入合并后的列表中,直到其中一个子列表为空,然后将另一个子列表的剩余部分直接放入合并后的列表中。</li>
</ol>
<img decoding="async" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/mergeSort.gif"></div><div class="tab-item-content" id="test1-6"><div class="note info no-icon flat"><p> 快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要 Ο(nlogn) 次比较。在最坏状况下则需要 Ο(n2) 次比较,但这种状况并不常见。事实上,快速排序通常明显比其他 Ο(nlogn) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来。</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>选择基准</strong>:从列表中选择一个元素作为基准元素(一般情况下是选择数组的第一个或最后一个)。</li>
<li><strong>分区</strong>:将列表中小于基准元素的元素放在基准元素的左边,大于基准元素的元素放在右边,基准元素放在中间。这个过程称为分区操作。</li>
<li><strong>递归排序</strong>:对左右两个子列表分别进行递归排序,直到列表为空或只包含一个元素。</li>
<li><strong>合并</strong>:由于快速排序是原地排序算法,不需要显式的合并步骤。</li>
</ol>
<p><strong>再详细一点:</strong></p>
<ol>
<li>原数组:[3, 5, 1, 9, 7, 2, 8, 4, 6]</li>
<li>选择基准元素 3,分成左半部分 [1, 2] 和右半部分 [5, 9, 7, 8, 4, 6]</li>
<li>对左半部分 [1, 2] 递归调用快速排序算法,得到有序数组 [1, 2]</li>
<li>对右半部分 [5, 9, 7, 8, 4, 6] 选择基准元素 5,分成左半部分 [4] 和右半部分 [9, 7, 8, 6]</li>
<li>对左半部分 [4] 递归调用快速排序算法,得到有序数组 [4]</li>
<li>对右半部分 [9, 7, 8, 6] 选择基准元素 9,分成左半部分 [7, 8, 6] 和右半部分 []</li>
<li>对左半部分 [7, 8, 6] 选择基准元素 7,分成左半部分 [6] 和右半部分 [8]</li>
<li>对左半部分 [6] 递归调用快速排序算法,得到有序数组 [6]</li>
<li>对右半部分 [8] 递归调用快速排序算法,得到有序数组 [8]</li>
<li>将左半部分 [6]、基准元素 7 和右半部分 [8] 拼接起来,得到有序数组 [6, 7, 8]</li>
<li>将左半部分 [4]、基准元素 5 和右半部分 [6, 7, 8, 9] 拼接起来,得到有序数组 [4, 5, 6, 7, 8, 9]</li>
<li>将左半部分 [1, 2]、基准元素 3 和右半部分 [4, 5, 6, 7, 8, 9] 拼接起来,得到有序数组 [1, 2, 3, 4, 5, 6, 7, 8, 9]</li>
</ol>
<img decoding="async" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/quickSort.gif" alt=""></div><div class="tab-item-content" id="test1-7"><div class="note info no-icon flat"><p> 堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。堆排序可以说是一种利用堆的概念来排序的选择排序。分为两种方法:</p>
<ul>
<li>大顶堆:每个节点的值都大于或等于其子节点的值,在堆排序算法中用于升序排列;</li>
<li>小顶堆:每个节点的值都小于或等于其子节点的值,在堆排序算法中用于降序排列;</li>
</ul>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>建立堆</strong>:将待排序的列表构建成一个最大堆。这一步通常是从列表的中间位置开始,自底向上地调整每个节点,使得整个列表满足堆的性质。</li>
<li><strong>排序</strong>:将最大堆中的根节点(即最大值)与堆中的最后一个元素交换位置,并将堆的大小减1。交换后可能会破坏堆的性质,因此需要进行堆调整操作,使得堆重新满足堆的性质。</li>
<li><strong>重复步骤2</strong>,直到堆的大小为1,此时整个列表已经排序完成。</li>
</ol>
<ul>
<li>
<img decoding="async" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/heapSort.gif">
<img decoding="async" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/Sorting_heapsort_anim.gif">
</li>
</ul></div><div class="tab-item-content" id="test1-8"><div class="note info no-icon flat"><p> 计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>统计元素出现次数</strong>:遍历待排序的列表,统计每个元素出现的次数,并将统计结果存储在一个额外的计数数组中。计数数组的索引对应于待排序元素的值,而数组中的值对应于该元素出现的次数。</li>
<li><strong>累加计数</strong>:对计数数组进行累加操作,使得每个索引位置的值等于其前面所有索引位置的值之和。这一步可以使得计数数组中的值表示小于等于该索引值的元素个数。</li>
<li><strong>输出排序结果</strong>:根据计数数组中的统计结果,将待排序列表中的元素放置到正确的位置上,最终得到排序结果。</li>
</ol>
<img decoding="async" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/countingSort.gif"></div><div class="tab-item-content" id="test1-9"><div class="note info no-icon flat"><p> 桶排序是计数排序的升级版。它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定。为了使桶排序更加高效,我们需要做到这两点:</p>
<p>在额外空间充足的情况下,尽量增大桶的数量<br>
使用的映射函数能够将输入的 N 个数据均匀的分配到 K 个桶中</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>划分桶</strong>:根据待排序元素的范围,确定一定数量的桶,并将待排序元素分配到相应的桶中。</li>
<li><strong>桶内排序</strong>:对每个桶中的元素进行排序。可以使用任何适合的排序算法,如插入排序、快速排序等。</li>
<li><strong>合并结果</strong>:按照桶的顺序依次取出各个桶中的元素,即可得到有序的结果。</li>
</ol></div><div class="tab-item-content" id="test1-10"><div class="note info no-icon flat"><p> 基数排序是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较。由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数,所以基数排序也不是只能使用于整数。</p>
</div>
<p><strong>基本步骤:</strong></p>
<ol>
<li><strong>按位排序</strong>:从最低位开始,对待排序的元素按照当前位数进行排序。可以使用稳定的排序算法,如计数排序或桶排序。</li>
<li><strong>重复排序</strong>:重复第一步的过程,对每一位进行排序,直到对最高位进行排序。</li>
<li><strong>合并结果</strong>:完成所有位的排序后,即可得到有序的结果。</li>
</ol>
<img decoding="async" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="//www.runoob.com/wp-content/uploads/2019/03/radixSort.gif" alt=""></div></div><div class="tab-to-top"><button type="button" aria-label="scroll to top"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></button></div></div>
<h1>总结</h1>
<table>
<thead>
<tr>
<th style="text-align:center">排序算法</th>
<th style="text-align:center">平均时间复杂度</th>
<th style="text-align:center">最好情况</th>
<th style="text-align:center">最坏情况</th>
<th style="text-align:center">空间复杂度</th>
<th style="text-align:center">排序方式</th>
<th style="text-align:center">稳定性</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">冒泡排序</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(n)</td>
<td style="text-align:center">O(n)</td>
<td style="text-align:center">O(1)</td>
<td style="text-align:center">In-place</td>
<td style="text-align:center">稳定</td>
</tr>
<tr>
<td style="text-align:center">选择排序</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(1)</td>
<td style="text-align:center">In-place</td>
<td style="text-align:center">不稳定</td>
</tr>
<tr>
<td style="text-align:center">插入排序</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(n)</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(1)</td>
<td style="text-align:center">In-place</td>
<td style="text-align:center">稳定</td>
</tr>
<tr>
<td style="text-align:center">希尔排序</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(n log^2 n)</td>
<td style="text-align:center">O(n log^2 n)</td>
<td style="text-align:center">O(1)</td>
<td style="text-align:center">In-place</td>
<td style="text-align:center">不稳定</td>
</tr>
<tr>
<td style="text-align:center">归并排序</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(n logn)</td>
<td style="text-align:center">O(n)</td>
<td style="text-align:center">Out-place</td>
<td style="text-align:center">稳定</td>
</tr>
<tr>
<td style="text-align:center">快速排序</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(log n)</td>
<td style="text-align:center">In-place</td>
<td style="text-align:center">不稳定</td>
</tr>
<tr>
<td style="text-align:center">堆排序</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(n log n)</td>
<td style="text-align:center">O(1)</td>
<td style="text-align:center">In-place</td>
<td style="text-align:center">不稳定</td>
</tr>
<tr>
<td style="text-align:center">计数排序</td>
<td style="text-align:center">O(n+k)</td>
<td style="text-align:center">O(n+k)</td>
<td style="text-align:center">O(n+k)</td>
<td style="text-align:center">O(k)</td>
<td style="text-align:center">Out-place</td>
<td style="text-align:center">稳定</td>
</tr>
<tr>
<td style="text-align:center">桶排序</td>
<td style="text-align:center">O(n+k)</td>
<td style="text-align:center">O(n+k)</td>
<td style="text-align:center">O(n^2)</td>
<td style="text-align:center">O(n+k)</td>
<td style="text-align:center">Out-place</td>
<td style="text-align:center">稳定</td>
</tr>
<tr>
<td style="text-align:center">基数排序</td>
<td style="text-align:center">O(n×k)</td>
<td style="text-align:center">O(n×k)</td>
<td style="text-align:center">O(n×k)</td>
<td style="text-align:center">O(n+k)</td>
<td style="text-align:center">Out-place</td>
<td style="text-align:center">稳定</td>
</tr>
</tbody>
</table>
</article><div class="post-copyright"><div class="copyright-cc-box"><i class="anzhiyufont anzhiyu-icon-copyright"></i></div><div class="post-copyright__author_box"><a class="post-copyright__author_img" href="/" title="头像"><img class="post-copyright__author_img_back" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/avatar.png" title="头像" alt="头像"><img class="post-copyright__author_img_front" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/avatar.png" title="头像" alt="头像"></a><div class="post-copyright__author_name">伴随</div><div class="post-copyright__author_desc">请保持那份热爱 奔赴下一场山海</div></div><div class="post-copyright__post__info"><a class="post-copyright__original" title="该文章为原创文章,注意版权协议" href="https://ctext.top/b78b.html">原创</a><a class="post-copyright-title"><span onclick="rm.copyPageUrl('https://ctext.top/b78b.html')">排序算法详解</span></a></div><div class="post-tools" id="post-tools"><div class="post-tools-left"><div class="rewardLeftButton"><div class="post-reward" onclick="anzhiyu.addRewardMask()"><div class="reward-button button--animated" title="赞赏作者"><i class="anzhiyufont anzhiyu-icon-hand-heart-fill"></i>打赏作者</div><div class="reward-main"><div class="reward-all"><span class="reward-title">感谢你赐予我前进的力量</span><ul class="reward-group"><li class="reward-item"><a href="https://up.ctext.top/pay/wx.png" target="_blank"><img class="post-qr-code-img" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/pay/wx.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://up.ctext.top/pay/zfb.png" target="_blank"><img class="post-qr-code-img" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/pay/zfb.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul><a class="reward-main-btn" href="/about/#about-reward" target="_blank"><div class="reward-text">赞赏者名单</div><div class="reward-dec">因为你们的支持让我意识到写文章的价值🙏</div></a></div></div></div><div id="quit-box" onclick="anzhiyu.removeRewardMask()" style="display: none"></div></div><div class="shareRight"><div class="share-link mobile"><div class="share-qrcode"><div class="share-button" title="使用手机访问这篇文章"><i class="anzhiyufont anzhiyu-icon-qrcode"></i></div><div class="share-main"><div class="share-main-all"><div id="qrcode" title="https://ctext.top/b78b.html"></div><div class="reward-dec">使用手机访问这篇文章</div></div></div></div></div><div class="share-link weibo"><a class="share-button" target="_blank" href="https://service.weibo.com/share/share.php?title=排序算法详解&url=https://ctext.top/b78b.html&pic=https://up.ctext.top/article/2024/04/sort.png" rel="external nofollow noreferrer noopener"><i class="anzhiyufont anzhiyu-icon-weibo"></i></a></div><script>function copyCurrentPageUrl() {
var currentPageUrl = window.location.href;
var input = document.createElement("input");
input.setAttribute("value", currentPageUrl);
document.body.appendChild(input);
input.select();
input.setSelectionRange(0, 99999);
document.execCommand("copy");
document.body.removeChild(input);
}</script><div class="share-link copyurl"><div class="share-button" id="post-share-url" title="复制链接" onclick="copyCurrentPageUrl()"><i class="anzhiyufont anzhiyu-icon-link"></i></div></div></div></div></div><div class="post-copyright__notice"><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ctext.top" target="_blank">伴随的小站</a>!</span></div></div><div class="post-tools-right"><div class="tag_share"><div class="post-meta__box"><div class="post-meta__box__category-list"><a class="post-meta__box__categoryes" href="/categories/%E8%BD%AF%E8%80%83%E7%AC%94%E8%AE%B0/"><span class="categoryes-punctuation"> <i class="anzhiyufont anzhiyu-icon-inbox"></i></span>软考笔记<span class="categoryesPageCount">5</span></a></div><div class="post-meta__box__tag-list"><a class="post-meta__box__tags" href="/tags/%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E5%B8%88/"><span class="tags-punctuation"> <i class="anzhiyufont anzhiyu-icon-tag"></i></span>软件设计师<span class="tagsPageCount">5</span></a><a class="post-meta__box__tags" href="/tags/%E8%BD%AF%E8%80%83/"><span class="tags-punctuation"> <i class="anzhiyufont anzhiyu-icon-tag"></i></span>软考<span class="tagsPageCount">3</span></a><a class="post-meta__box__tags" href="/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/"><span class="tags-punctuation"> <i class="anzhiyufont anzhiyu-icon-tag"></i></span>排序算法<span class="tagsPageCount">1</span></a></div></div><div class="post_share"><div class="social-share" data-image="https://up.ctext.top/article/2024/11/tools3.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.cbd.int/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"/><script src="https://cdn.cbd.int/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer="defer"></script></div></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/ebff.html"><img class="prev-cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/04/bolan.png" onerror="onerror=null;src='https://up.ctext.top/default_bg.png'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">一文搞懂波兰表达式</div></div></a></div><div class="next-post pull-right"><a href="/fb6a.html"><img class="next-cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/05/hidn.png" onerror="onerror=null;src='https://up.ctext.top/default_bg.png'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">hexo配置隐藏文章</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="anzhiyufont anzhiyu-icon-thumbs-up fa-fw" style="font-size: 1.5rem; margin-right: 4px"></i><span>喜欢这篇文章的人也看了</span></div><div class="relatedPosts-list"><div><a href="/ebff.html" title="一文搞懂波兰表达式"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/04/bolan.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2024-04-05</div><div class="title">一文搞懂波兰表达式</div></div></a></div><div><a href="/eaa2.html" title="一文搞懂奇偶校验、海明码和CRC"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/03/verify.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2024-03-27</div><div class="title">一文搞懂奇偶校验、海明码和CRC</div></div></a></div><div><a href="/579f.html" title="软件设计师考点总结"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/02/software.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2024-02-26</div><div class="title">软件设计师考点总结</div></div></a></div><div><a href="/782d.html" title="一文搞懂原码、反码、补码、移码"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/03/number.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2024-03-23</div><div class="title">一文搞懂原码、反码、补码、移码</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="anzhiyufont anzhiyu-icon-comments"></i><span> 评论</span></div><div class="comment-randomInfo"><a onclick="anzhiyu.addRandomCommentInfo()" href="javascript:void(0)" style="display: none">匿名评论</a><a href="/privacy" style="margin-left: 4px">隐私政策</a></div></div><div class="comment-wrap"><div><div id="waline-wrap"></div></div></div></div><div class="comment-barrage"></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-content"><div class="author-info__sayhi" id="author-info__sayhi" onclick="anzhiyu.changeSayHelloText()"></div><div class="author-info-avatar"><img class="avatar-img" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/avatar.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-status"><img class="g-status" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://bu.dusays.com/2023/08/24/64e6ce9c507bb.png" alt="status"/></div></div><div class="author-info__description"><div style="line-height:1.38;margin:0.6rem 0;text-align:center;color:rgba(255, 255, 255, 0.8);"> <b style="color:#fff">我想,左肩有你,右肩微笑</b> </div> <div style="line-height:1.38;margin:0.6rem 0;text-align:center;color:rgba(255, 255, 255, 0.8);"> <b style="color:#fff">我想,在你眼里,撒野奔跑</b> </div> <div style="line-height:1.38;margin:0.6rem 0;text-align:center;color:rgba(255, 255, 255, 0.8);"> <b style="color:#fff">我想,一个眼神,就到老 </b> </div> <div style="line-height:1.38;margin:0.6rem 0;text-align:center;color:rgba(255, 255, 255, 0.8);"> <b style="color:#fff">我想………        </b> </div></div><div class="author-info__bottom-group"><a class="author-info__bottom-group-left" href="/"><h1 class="author-info__name">伴随</h1><div class="author-info__desc">请保持那份热爱 奔赴下一场山海</div></a><div class="card-info-social-icons is-center"><a class="social-icon faa-parent animated-hover" href="https://qm.qq.com/q/1sv8urqpRS" target="_blank" title="QQ"><i class="anzhiyufont anzhiyu-icon-qq"></i></a></div></div></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-bullhorn anzhiyu-shake"></i><span>公告</span></div><div class="announcement_content"><div id="welcome-info"></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-bars"></i><span>文章目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">小知识</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%E6%98%AF%E5%90%A6%E7%A8%B3%E5%AE%9A"><span class="toc-number">1.0.1.</span> <span class="toc-text">如何判断一个排序算法是否稳定</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%9F%E8%AE%B0"><span class="toc-number">1.0.2.</span> <span class="toc-text">速记</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%92%E5%BA%8F%E6%96%B9%E5%BC%8F"><span class="toc-number">1.0.3.</span> <span class="toc-text">排序方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%97%E6%B3%95%E7%AD%96%E7%95%A5"><span class="toc-number">1.0.4.</span> <span class="toc-text">算法策略</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">2.</span> <span class="toc-text">算法详解</span></a></li><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">3.</span> <span class="toc-text">总结</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-history"></i><span>最近发布</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/8a0c.html" title="JAVA表达式解析器———EvalEx"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/11/tools3.png" onerror="this.onerror=null;this.src='https://up.ctext.top/default_bg.png'" alt="JAVA表达式解析器———EvalEx"/></a><div class="content"><a class="title" href="/8a0c.html" title="JAVA表达式解析器———EvalEx">JAVA表达式解析器———EvalEx</a><time datetime="2024-11-17T06:10:54.000Z" title="发表于 2024-11-17 14:10:54">2024-11-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/dfd4.html" title="IDEA快捷键大全"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/10/idea.png" onerror="this.onerror=null;this.src='https://up.ctext.top/default_bg.png'" alt="IDEA快捷键大全"/></a><div class="content"><a class="title" href="/dfd4.html" title="IDEA快捷键大全">IDEA快捷键大全</a><time datetime="2024-10-15T12:08:03.000Z" title="发表于 2024-10-15 20:08:03">2024-10-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/51a7.html" title="java随机数介绍以及生成随机日期"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/10/tools2.png" onerror="this.onerror=null;this.src='https://up.ctext.top/default_bg.png'" alt="java随机数介绍以及生成随机日期"/></a><div class="content"><a class="title" href="/51a7.html" title="java随机数介绍以及生成随机日期">java随机数介绍以及生成随机日期</a><time datetime="2024-10-14T12:16:33.000Z" title="发表于 2024-10-14 20:16:33">2024-10-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/3329.html" title="JAVA工具类(一)——生成班次排班记录"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/08/tools1.png" onerror="this.onerror=null;this.src='https://up.ctext.top/default_bg.png'" alt="JAVA工具类(一)——生成班次排班记录"/></a><div class="content"><a class="title" href="/3329.html" title="JAVA工具类(一)——生成班次排班记录">JAVA工具类(一)——生成班次排班记录</a><time datetime="2024-08-15T12:34:41.000Z" title="发表于 2024-08-15 20:34:41">2024-08-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/43a1.html" title="Feign调用者如何配置全局Header参数?"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/article/2024/08/feign.png" onerror="this.onerror=null;this.src='https://up.ctext.top/default_bg.png'" alt="Feign调用者如何配置全局Header参数?"/></a><div class="content"><a class="title" href="/43a1.html" title="Feign调用者如何配置全局Header参数?">Feign调用者如何配置全局Header参数?</a><time datetime="2024-08-12T11:39:34.000Z" title="发表于 2024-08-12 19:39:34">2024-08-12</time></div></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div id="workboard"><img class="workSituationImg boardsign" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/happy.svg" alt="下班了就该开开心心的玩耍,嘿嘿~" title="下班了就该开开心心的玩耍,嘿嘿~"/><div id="runtimeTextTip"></div></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" style="margin-inline:5px" data-title="博客框架为Hexo_v7.2.0" title="博客框架为Hexo_v7.2.0"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/svg/hexo.svg" alt="博客框架为Hexo_v7.2.0"/></a><a class="github-badge" target="_blank" href="https://docs.anheyu.com/" style="margin-inline:5px" data-title="本站使用AnZhiYu主题" title="本站使用AnZhiYu主题"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/svg/anzhiyu.svg" alt="本站使用AnZhiYu主题"/></a><a class="github-badge" target="_blank" href="https://vercel.com/" style="margin-inline:5px" data-title="本站使用Vercel部署" title="本站使用Vercel部署"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/svg/vercel.svg" alt="本站使用Vercel部署"/></a><a class="github-badge" target="_blank" href="https://github.com/" style="margin-inline:5px" data-title="本站项目源码由Github托管" title="本站项目源码由Github托管"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/svg/github.svg" alt="本站项目源码由Github托管"/></a><a class="github-badge" target="_blank" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" style="margin-inline:5px" data-title="本站项目由又拍云提供CDN加速/云存储服务" title="本站项目由又拍云提供CDN加速/云存储服务"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/svg/up.svg" alt="本站项目由又拍云提供CDN加速/云存储服务"/></a><a class="github-badge" target="_blank" href="https://github.com/walinejs/waline" style="margin-inline:5px" data-title="本站评论系统使用Waline搭建" title="本站评论系统使用Waline搭建"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/svg/waline.svg" alt="本站评论系统使用Waline搭建"/></a></p></div><div id="footer-bar"><div class="footer-bar-links"><div class="footer-bar-left"><div id="footer-bar-tips"><div class="copyright">©2024 - 2025 By <a class="footer-bar-link" href="/" title="伴随" target="_blank">伴随</a></div></div><div id="footer-type-tips"></div><div class="js-pjax"><script>function subtitleType () {
getScript('https://yijuzhan.com/api/word.php?m=js').then(() => {
const con = str[0]
if (true) {
const from = '出自 ' + str[1]
const sub = []
sub.unshift(con, from)
window.typed = new Typed('#footer-type-tips', {
strings: sub,
startDelay: 300,
typeSpeed: 150,
loop: true,
backSpeed: 50,
})
} else {
document.getElementById('footer-type-tips').innerHTML = con
}
})
}
if (true) {
if (typeof Typed === 'function') {
subtitleType()
} else {
getScript('https://cdn.cbd.int/typed.js@2.1.0/dist/typed.umd.js').then(subtitleType)
}
} else {
subtitleType()
}
</script></div></div><div class="footer-bar-right"><a class="footer-bar-link" target="_blank" rel="noopener" href="https://beian.miit.gov.cn/" title="京ICP备2022017849号-2">京ICP备2022017849号-2</a></div></div></div></footer></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="sidebar-site-data site-data is-center"><a href="/archives/" title="archive"><div class="headline">文章</div><div class="length-num">23</div></a><a href="/tags/" title="tag"><div class="headline">标签</div><div class="length-num">21</div></a><a href="/categories/" title="category"><div class="headline">分类</div><div class="length-num">6</div></a></div><span class="sidebar-menu-item-title">功能</span><div class="sidebar-menu-item"><a class="darkmode_switchbutton menu-child" href="javascript:void(0);" title="显示模式"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i><span>显示模式</span></a></div><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">关于</div><div class="back-menu-list"><a class="back-menu-item" href="/about/" title="我"><img class="back-menu-item-icon" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/ico/orange.svg" alt="我"/><span class="back-menu-item-text">我</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">博客</div><div class="back-menu-list"><a class="back-menu-item" href="/album/" title="我的相册"><img class="back-menu-item-icon" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/ico/strawberry.svg" alt="我的相册"/><span class="back-menu-item-text">我的相册</span></a><a class="back-menu-item" href="/essay/" title="记录生活"><img class="back-menu-item-icon" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="this.onerror=null,this.src="https://up.ctext.top/default_bg.png"" data-lazy-src="https://up.ctext.top/ico/apple.svg" alt="记录生活"/><span class="back-menu-item-text">记录生活</span></a></div></div></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 文章</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/"><i class="anzhiyufont anzhiyu-icon-grip-vertical faa-tada" style="font-size: 0.9em;"></i><span> 主页</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/archives/"><i class="anzhiyufont anzhiyu-icon-box-archive faa-tada" style="font-size: 0.9em;"></i><span> 总览</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><i class="anzhiyufont anzhiyu-icon-shapes faa-tada" style="font-size: 0.9em;"></i><span> 分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><i class="anzhiyufont anzhiyu-icon-tags faa-tada" style="font-size: 0.9em;"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 我的</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/growth.html"><i class="anzhiyufont anzhiyu-icon-heartbeat faa-tada" style="font-size: 0.9em;"></i><span> 博客岁月</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/music/"><i class="anzhiyufont anzhiyu-icon-music faa-tada" style="font-size: 0.9em;"></i><span> 音乐馆</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/album/"><i class="anzhiyufont anzhiyu-icon-images faa-tada" style="font-size: 0.9em;"></i><span> 相册集</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/essay/"><i class="anzhiyufont anzhiyu-icon-lightbulb faa-tada" style="font-size: 0.9em;"></i><span> 记录生活</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 小工具</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/air-conditioner/"><i class="anzhiyufont anzhiyu-icon-fan faa-tada" style="font-size: 0.9em;"></i><span> 小空调</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/gradation/"><i class="anzhiyufont anzhiyu-icon-envelope faa-tada" style="font-size: 0.9em;"></i><span> 渐变背景生成</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/9abe.html"><i class="anzhiyufont anzhiyu-icon-artstation faa-tada" style="font-size: 0.9em;"></i><span> tag-plugins文档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/nav"><i class="anzhiyufont anzhiyu-icon-link faa-tada" style="font-size: 0.9em;"></i><span> 网址导航</span></a></li><li><a class="site-page child faa-parent animated-hover" target="_blank" rel="noopener" href="https://map.ctext.top"><i class="anzhiyufont anzhiyu-icon-keyboard faa-tada" style="font-size: 0.9em;"></i><span> 飞车随机选图工具</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><span> 关于</span></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/about/"><i class="anzhiyufont anzhiyu-icon-paper-plane faa-tada" style="font-size: 0.9em;"></i><span> 关于自己</span></a></li><li><a class="site-page child faa-parent animated-hover" href="javascript:toRandomPost()"><i class="anzhiyufont anzhiyu-icon-shoe-prints1 faa-tada" style="font-size: 0.9em;"></i><span> 随便逛逛</span></a></li></ul></div></div><span class="sidebar-menu-item-title">标签</span><div class="card-tags"><div class="item-headline"></div><div class="card-tag-cloud"><a href="/tags/Feign/" style="font-size: 0.88rem;">Feign<sup>1</sup></a><a href="/tags/IDEA/" style="font-size: 0.88rem;">IDEA<sup>1</sup></a><a href="/tags/JAVA/" style="font-size: 0.88rem;">JAVA<sup>5</sup></a><a href="/tags/Waline/" style="font-size: 0.88rem;">Waline<sup>1</sup></a><a href="/tags/docker/" style="font-size: 0.88rem;">docker<sup>1</sup></a><a href="/tags/hexo/" style="font-size: 0.88rem;">hexo<sup>4</sup></a><a href="/tags/java/" style="font-size: 0.88rem;">java<sup>1</sup></a><a href="/tags/linux/" style="font-size: 0.88rem;">linux<sup>2</sup></a><a href="/tags/logback/" style="font-size: 0.88rem;">logback<sup>1</sup></a><a href="/tags/nacos/" style="font-size: 0.88rem;">nacos<sup>1</sup></a><a href="/tags/window/" style="font-size: 0.88rem;">window<sup>1</sup></a><a href="/tags/%E4%BA%8C%E8%BF%9B%E5%88%B6/" style="font-size: 0.88rem;">二进制<sup>1</sup></a><a href="/tags/%E5%B7%A5%E5%85%B7%E7%B1%BB/" style="font-size: 0.88rem;">工具类<sup>3</sup></a><a href="/tags/%E5%BE%AE%E4%BF%A1/" style="font-size: 0.88rem;">微信<sup>1</sup></a><a href="/tags/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/" style="font-size: 0.88rem;">排序算法<sup>1</sup></a><a href="/tags/%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E/" style="font-size: 0.88rem;">搜索引擎<sup>1</sup></a><a href="/tags/%E7%AC%A6%E5%8F%B7%E7%9A%84%E8%89%BA%E6%9C%AF/" style="font-size: 0.88rem;">符号的艺术<sup>1</sup></a><a href="/tags/%E8%84%9A%E6%9C%AC/" style="font-size: 0.88rem;">脚本<sup>2</sup></a><a href="/tags/%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E5%B8%88/" style="font-size: 0.88rem;">软件设计师<sup>5</sup></a><a href="/tags/%E8%BD%AF%E8%80%83/" style="font-size: 0.88rem;">软考<sup>3</sup></a><a href="/tags/%E8%BE%BE%E6%A2%A6%E6%95%B0%E6%8D%AE%E5%BA%93/" style="font-size: 0.88rem;">达梦数据库<sup>1</sup></a></div></div><hr/></div></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="anzhiyufont anzhiyu-icon-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="anzhiyufont anzhiyu-icon-gear"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="anzhiyufont anzhiyu-icon-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="anzhiyufont anzhiyu-icon-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></button></div></div><div id="nav-music"><a id="nav-music-hoverTips" onclick="anzhiyu.musicToggle()" accesskey="m">播放音乐</a><div id="console-music-bg"></div><meting-js id="2788494248" server="netease" type="playlist" mutex="true" preload="none" theme="var(--anzhiyu-main)" data-lrctype="0" order="random" volume="0.7"></meting-js></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="anzhiyufont anzhiyu-icon-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="anzhiyufont anzhiyu-icon-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="anzhiyufont anzhiyu-icon-arrow-rotate-right" style="font-size: 1rem;"></i></div><div class="rightMenu-item" id="menu-top"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuPlugin"><div class="rightMenu-item" id="menu-copytext"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制选中文本</span></div><div class="rightMenu-item" id="menu-pastetext"><i class="anzhiyufont anzhiyu-icon-paste"></i><span>粘贴文本</span></div><a class="rightMenu-item" id="menu-commenttext"><i class="anzhiyufont anzhiyu-icon-comment-medical"></i><span>引用到评论</span></a><div class="rightMenu-item" id="menu-newwindow"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开</span></div><div class="rightMenu-item" id="menu-copylink"><i class="anzhiyufont anzhiyu-icon-link"></i><span>复制链接地址</span></div><div class="rightMenu-item" id="menu-copyimg"><i class="anzhiyufont anzhiyu-icon-images"></i><span>复制此图片</span></div><div class="rightMenu-item" id="menu-downloadimg"><i class="anzhiyufont anzhiyu-icon-download"></i><span>下载此图片</span></div><div class="rightMenu-item" id="menu-newwindowimg"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开图片</span></div><div class="rightMenu-item" id="menu-search"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>站内搜索</span></div><div class="rightMenu-item" id="menu-searchBaidu"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>百度搜索</span></div><div class="rightMenu-item" id="menu-music-toggle"><i class="anzhiyufont anzhiyu-icon-play"></i><span>播放音乐</span></div><div class="rightMenu-item" id="menu-music-back"><i class="anzhiyufont anzhiyu-icon-backward"></i><span>切换到上一首</span></div><div class="rightMenu-item" id="menu-music-forward"><i class="anzhiyufont anzhiyu-icon-forward"></i><span>切换到下一首</span></div><div class="rightMenu-item" id="menu-music-playlist" onclick="window.open("https://music.163.com/#/playlist?id=2788494248", "_blank");" style="display: none;"><i class="anzhiyufont anzhiyu-icon-radio"></i><span>查看所有歌曲</span></div><div class="rightMenu-item" id="menu-music-copyMusicName"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制歌名</span></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" id="menu-randomPost"><i class="anzhiyufont anzhiyu-icon-shuffle"></i><span>随便逛逛</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="anzhiyufont anzhiyu-icon-cube"></i><span>博客分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="anzhiyufont anzhiyu-icon-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item" id="menu-copy" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制地址</span></a><a class="rightMenu-item" id="menu-commentBarrage" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-message"></i><span class="menu-commentBarrage-text">关闭热评</span></a><a class="rightMenu-item" id="menu-darkmode" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i><span class="menu-darkmode-text">深色模式</span></a><a class="rightMenu-item" id="menu-translate" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-language"></i><span>轉為繁體</span></a></div></div><div id="rightmenu-mask"></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.cbd.int/@fancyapps/ui@5.0.28/dist/fancybox/fancybox.umd.js"></script><script src="https://cdn.cbd.int/instant.page@5.2.0/instantpage.js" type="module"></script><script src="https://cdn.cbd.int/vanilla-lazyload@17.8.5/dist/lazyload.iife.min.js"></script><script src="https://cdn.cbd.int/node-snackbar@0.1.16/dist/snackbar.min.js"></script><script>function panguFn () {
if (typeof pangu === 'object') pangu.autoSpacingPage()
else {
getScript('https://cdn.cbd.int/pangu@4.0.7/dist/browser/pangu.min.js')
.then(() => {
pangu.autoSpacingPage()
})
}
}
function panguInit () {
if (false){
GLOBAL_CONFIG_SITE.isPost && panguFn()
} else {
panguFn()
}
}
document.addEventListener('DOMContentLoaded', panguInit)</script><canvas id="universe"></canvas><script async src="https://npm.elemecdn.com/anzhiyu-theme-static@1.0.0/dark/dark.js"></script><script async src="/anzhiyu/random.js"></script><script async="async">(function () {
var grt = new Date("01/02/2024 00:00:00"); //设置网站上线时间
var now = new Date();
var dnum;
var hnum;
var mnum;
var snum;
var nowHour;
// 计算并更新天数、小时数、分钟数和秒数
function updateTime() {
now = new Date(); // 更新 now 的值
nowHour = now.getHours(); // 更新 nowHour 的值
var days = (now - grt) / 1000 / 60 / 60 / 24;
dnum = Math.floor(days);
var hours = (now - grt) / 1000 / 60 / 60 - 24 * dnum;
hnum = Math.floor(hours);
if (String(hnum).length == 1) {
hnum = "0" + hnum;
}
var minutes = (now - grt) / 1000 / 60 - 24 * 60 * dnum - 60 * hnum;
mnum = Math.floor(minutes);
if (String(mnum).length == 1) {
mnum = "0" + mnum;
}
var seconds = (now - grt) / 1000 - 24 * 60 * 60 * dnum - 60 * 60 * hnum - 60 * mnum;
snum = Math.round(seconds);
if (String(snum).length == 1) {
snum = "0" + snum;
}
}
// 更新网页中显示的网站运行时间
function updateHtml() {
const footer = document.getElementById("footer");
if (!footer) return
let currentTimeHtml = "";
if (nowHour < 18 && nowHour >= 9) {
// 如果是上班时间,默认就是"安知鱼-上班摸鱼中.svg"图片,不需要更改
currentTimeHtml = `本站居然运行了 ${dnum} 天<span id='runtime'> ${hnum} 小时 ${mnum} 分 ${snum} 秒 </span><i class='anzhiyufont anzhiyu-icon-heartbeat' style='color:red'></i>`;
} else {
// 如果是下班时间,插入"安知鱼-下班啦.svg"图片
let img = document.querySelector("#workboard .workSituationImg");
if (img != null) {
img.src = "https://up.ctext.top/happy.svg";
img.title = "下班了就该开开心心的玩耍,嘿嘿~";
img.alt = "下班了就该开开心心的玩耍,嘿嘿~";
}
currentTimeHtml = `本站居然运行了 ${dnum} 天<span id='runtime'> ${hnum} 小时 ${mnum} 分 ${snum} 秒 </span><i class='anzhiyufont anzhiyu-icon-heartbeat' style='color:red'></i>`;
}
if (document.getElementById("runtimeTextTip")) {
document.getElementById("runtimeTextTip").innerHTML = currentTimeHtml;
}
}
setInterval(() => {
updateTime();
updateHtml();
}, 1000);
})();</script><div class="js-pjax"><script>(() => {
const locale = {
nick: '你的专属昵称',
nickError: '昵称太短啦!至少得有3个字符才能算数哦!',
mail: '你的电子小信箱',
mailError: '哎呀,这不是一个合格的邮箱地址哦,请再检查一遍!',
link: '你有个酷酷的网址吗?',
optional: '这个嘛,想填就填,不填也行!',
placeholder: '吐槽一下?',
sofa: '来,坐沙发上,发表你的评论吧!',
submit: '一键发送,让世界听到你的声音!',
like: '点个赞,让TA知道你的心意!',
cancelLike: '哎呀,不小心点错了,取消赞!',
reply: '回复TA,开启一场对话吧!',
cancelReply: '等等,我话还没说完,取消回复!',
comment: '来,说说你的看法,让评论区热闹起来!',
refresh: '刷新一下,看看有啥新动态!',
more: '哇塞,还有更多精彩内容等你来加载!',
preview: '先睹为快,预览一下你的大作吧!',
emoji: '来,加点表情,让你的评论更生动!',
uploadImage: '上传你的美照,让大家一起欣赏!',
seconds: '刚刚才发的,新鲜出炉!',
minutes: '几分钟前,还热乎着呢!',
hours: '几小时前,热度还在哦!',
days: '几天前,但依旧值得一看!',
now: '哇塞,刚刚发的,快抢沙发!',
uploading: '稍等片刻,你的图片正在飞奔而来!',
login: '登录一下,解锁更多功能!',
logout: '拜拜了您嘞,下次再见!',
admin: '博主大人在此,谁敢造次!',
sticky: '置顶一下,让更多人看到你的精彩内容!',
word: '字',
wordHint: '评论字数要在0到1字之间哦!\n现在你已经写了$2个字了,加油!',
anonymous: '神秘人出没,猜猜是谁?',
level0: '潜水员,深藏不露!',
level1: '冒泡小能手,偶尔露个头!',
level2: '吐槽大师,一针见血!',
level3: '活跃分子,无处不在!',
level4: '话痨达人,说个不停!',
level5: '传说级大神,膜拜一下!',
gif: '动态表情包,让你的评论更有趣!',
gifSearchPlaceholder: '快来搜索你喜欢的表情包,让评论更生动!',
profile: '看看我的个人资料,了解一下我吧!',
approved: '审核通过,可以展示啦!',
waiting: '正在等待审核,别急哦!',
spam: '垃圾内容,走开走开!',
unsticky: '换个位置看看!',
oldest: '看看最早的评论!',
latest: '看看最新的动态!',
hottest: '看看哪条评论最火!',
reactionTitle: '快来对这篇文章发表你的看法吧,让博主知道你的想法!😜'
};
const initWaline = () => {
const waline = Waline.init(Object.assign({
el: '#waline-wrap',
serverURL: 'https://bc.ctext.top/',
pageview: false,
dark: 'html[data-theme="dark"]',
path: window.location.pathname,
turnstileKey: '0x4AAAAAAAexA2vW2PBFT3wv',
comment: false,
copyright: false,
reaction: true,
locale
}, null))
}
const loadWaline = async () => {
if (typeof Waline === 'object') initWaline()
else {
await getCSS('https://cdn.cbd.int/@waline/client@3.1.3/dist/waline.css')
await getScript('https://cdn.cbd.int/@waline/client@3.1.3/dist/waline.js')
initWaline()
}
}
if ('Waline' === 'Waline' || !true) {
if (true) anzhiyu.loadComment(document.getElementById('waline-wrap'),loadWaline)
else setTimeout(loadWaline, 0)
} else {
window.loadOtherComment = loadWaline
}
})()</script><input type="hidden" name="page-type" id="page-type" value="post"></div><script>window.addEventListener('load', () => {
const changeContent = content => {
if (content === '') return content
content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[图片]') // replace image link
content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[链接]') // replace url
content = content.replace(/<pre><code>.*?<\/pre>/gi, '[代码]') // replace code
content = content.replace(/<[^>]+>/g,"") // remove html tag
if (content.length > 150) {
content = content.substring(0,150) + '...'
}
return content
}
const generateHtml = array => {
let result = ''
if (array.length) {
for (let i = 0; i < array.length; i++) {
result += '<div class=\'aside-list-item\'>'
if (true) {
const name = 'data-lazy-src'
result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'></a>`
}
result += `<div class='content'>
<a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
<div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${anzhiyu.diffDate(array[i].date, true)}</time></div>
</div></div>`
}
} else {
result += '没有评论'
}
let $dom = document.querySelector('#card-newest-comments .aside-list')
$dom && ($dom.innerHTML= result)
window.lazyLoadInstance && window.lazyLoadInstance.update()
window.pjax && window.pjax.refresh($dom)
}
const getComment = async () => {
try {
const res = await fetch('https://bc.ctext.top/api/comment?type=recent&count=6', { method: 'GET' })
const result = await res.json()
const walineArray = result.data.map(e => {
return {
'content': changeContent(e.comment),
'avatar': e.avatar,
'nick': e.nick,
'url': e.url + '#' + e.objectId,
'date': e.time || e.insertedAt
}
})
saveToLocal.set('waline-newest-comments', JSON.stringify(walineArray), 10/(60*24))
generateHtml(walineArray)
} catch (err) {
console.error(err)
const $dom = document.querySelector('#card-newest-comments .aside-list')
$dom.textContent= "无法获取评论,请确认相关配置是否正确"
}
}
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('waline-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
getComment()
}
}
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
})</script><script async data-pjax src="https://cdn.cbd.int/anzhiyu-theme-static@1.0.0/waterfall/waterfall.js"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/qrcodejs/1.0.0/qrcode.min.js"></script><script src="/js/anzhiyu/right_click_menu.js"></script><link rel="stylesheet" href="https://cdn.cbd.int/anzhiyu-theme-static@1.1.9/icon/ali_iconfont_css.css"><script async src="https://at.alicdn.com/t/c/font_4489064_ow7l1rcvcw.js?spm=a313x.manage_type_myprojects.i1.12.61783a81xP1PqH&file=font_4489064_ow7l1rcvcw.js"></script><script async data-pjax src="/js/imgloaded.js?1"></script><script src="https://cdn.staticfile.org/jquery/3.6.3/jquery.min.js"></script><script defer="true" src="/js/welcome.js"></script><script defer="true" src="/js/hideRolling.js"></script><script defer="true" src="/js/countdown.js"></script><script defer src="https://cdn.jsdelivr.net/combine/npm/jquery@latest/dist/jquery.min.js,gh/weilining/jsdelivr/jquery/circlemagic/circlemagic.min.js,gh/weilining/jsdelivr@latest/jquery/circlemagic/butterflycirclemagic.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.cbd.int/butterfly-extsrc@1.1.3/dist/canvas-nest.min.js"></script><script id="click-heart" src="https://cdn.cbd.int/butterfly-extsrc@1.1.3/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.cbd.int/anzhiyu-theme-static@1.0.0/aplayer/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.cbd.int/anzhiyu-blog-static@1.0.1/js/APlayer.min.js"></script><script src="https://cdn.cbd.int/hexo-anzhiyu-music@1.0.1/assets/js/Meting2.min.js"></script><script src="https://cdn.cbd.int/pjax@0.2.8/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]
var pjax = new Pjax({
elements: 'a:not([target="_blank"])',
selectors: pjaxSelectors,
cacheBust: false,
analytics: false,
scrollRestoration: false
})
document.addEventListener('pjax:send', function () {
// removeEventListener scroll
anzhiyu.removeGlobalFnEvent('pjax')
anzhiyu.removeGlobalFnEvent('themeChange')
document.getElementById('rightside').classList.remove('rightside-show')
if (window.aplayers) {
for (let i = 0; i < window.aplayers.length; i++) {
if (!window.aplayers[i].options.fixed) {
window.aplayers[i].destroy()
}
}
}
typeof typed === 'object' && typed.destroy()
//reset readmode
const $bodyClassList = document.body.classList
$bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')
})
document.addEventListener('pjax:complete', function () {
window.refreshFn()
document.querySelectorAll('script[data-pjax]').forEach(item => {
const newScript = document.createElement('script')
const content = item.text || item.textContent || item.innerHTML || ""
Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
newScript.appendChild(document.createTextNode(content))
item.parentNode.replaceChild(newScript, item)
})
GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()
typeof panguInit === 'function' && panguInit()
// google analytics
typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});
// baidu analytics
typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);
typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()
// prismjs
typeof Prism === 'object' && Prism.highlightAll()
})
document.addEventListener('pjax:error', e => {
if (e.request.status === 404) {
pjax.loadUrl('/404.html')
}
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script charset="UTF-8" src="https://cdn.cbd.int/anzhiyu-theme-static@1.1.5/accesskey/accesskey.js"></script></div><div id="popup-window"><div class="popup-window-title">通知</div><div class="popup-window-divider"></div><div class="popup-window-content"><div class="popup-tip">你好呀</div><div class="popup-link"><i class="anzhiyufont anzhiyu-icon-arrow-circle-right"></i></div></div></div><!-- hexo injector body_end start --><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>