網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.代碼模板
// 希爾排序(Shell Sort) void ShellSort(SqList *L) { int i, j; int increment = L->length; // 先讓增量初始化為序列的長(zhǎng)度 do { increment = increment / 3 + 1; // 計(jì)算增量的值 for (i = increment + 1; i <= L->length; i ++ ) { if (L->arr[i] < L->arr[i - increment]) { // 如果L->[i]需要插入有序增量子表 L->arr[0] = L->arr[i]; // 暫存在哨兵位 for (j = i - increment; j > 0 && L->arr[0] < L->arr[j]; j -= increment) { // 遍歷增量子表,尋找插入位置 L->arr[j + increment] = L->arr[j]; } L->arr[j+increment] = L->arr[0]; // 插入 } } } while (increment > 1); }
2.算法介紹
希爾排序,又叫縮小增量排序,算法屬于插入類排序的進(jìn)階算法,采取跳躍分割的策略,將關(guān)鍵字較小的元素跳躍式的往前挪,大大減小了交換比較的次數(shù)。使得序列整體基本有序 ,即大的元素基本在后面,小的元素基本在前面,不大不小的元素基本在中間。
希爾排序的關(guān)鍵在于將序列中相隔某個(gè)“增量”的元素組成一個(gè)子序列,且序列的最后一個(gè)增量必須為1,這樣才能保證最后的結(jié)果是有序且正確的。但增量如何選擇為最佳,至今仍無(wú)定論。且由于元素是跳躍式移動(dòng)的,所有希爾排序是一個(gè)不穩(wěn)定的排序算法,其時(shí)間復(fù)雜度受到增量選擇的影響,最好為O(n^1.3) , 最壞為O(n*n)。
3.實(shí)例
#include <iostream> using namespace std; const int N = 100; typedef struct { int arr[N]; // 存儲(chǔ)待排序的序列 int length; // 存儲(chǔ)序列的長(zhǎng)度 } SqList; void ShellSort(SqList *L) { int i, j; int increment = L->length; do { increment = increment / 3 + 1; for (i = increment + 1; i <= L->length; i ++ ) { if (L->arr[i] < L->arr[i - increment]) { L->arr[0] = L->arr[i]; for (j = i - increment; j > 0 && L->arr[0] < L->arr[j]; j -= increment) L->arr[j + increment] = L->arr[j]; L->arr[j + increment] = L->arr[0]; } } } while (increment > 1); } int main() { SqList L; L.arr[1] = 50; L.arr[2] = 10; L.arr[3] = 90; L.arr[4] = 30; L.arr[5] = 70; L.arr[6] = 40; L.arr[7] = 80; L.arr[8] = 60; L.arr[9] = 20; L.length = 9; ShellSort(&L); for (int i = 1; i <= L.length; i ++ ) cout << L.arr[i] << " "; }
原文鏈接:https://blog.csdn.net/weixin_51424157/article/details/122344532
相關(guān)推薦
- 2022-05-21 C++多線程互斥鎖和條件變量的詳解_C 語(yǔ)言
- 2022-04-15 ASP.NET?Core基礎(chǔ)之中間件_基礎(chǔ)應(yīng)用
- 2022-07-10 Spring依賴注入的幾種方式詳解
- 2022-03-12 使用VMware虛擬機(jī)安裝Linux(CentOS7鏡像)_Linux
- 2022-09-05 Shiro和Security的核心模塊以及兩者的區(qū)別
- 2023-01-10 Go語(yǔ)言rune與字符串轉(zhuǎn)換的密切關(guān)系解析_Golang
- 2022-08-10 C++中string字符串分割函數(shù)split()的4種實(shí)現(xiàn)方法_C 語(yǔ)言
- 2022-09-03 機(jī)器學(xué)習(xí)數(shù)據(jù)預(yù)處理之獨(dú)熱One-Hot編碼及其代碼詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支