zoukankan
html css js c++ java
插入排序之表插入
表插入
时间复杂度O(n^2)
附加空间O(1)
稳定排序
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #define LEN 8 // 有LEN个元素要排 struct Record { // 为了考察排序的稳定性,定义元素是结构体类型 int key; int otherinfo; int next; }; void LinkListInsertSort(Record *arr, int length) // length是要排序的元素的个数,0号单元除外 { for (int i = 2; i <= length; ++i) { int q = 0; // q跟在p之后,以方便插入结点(插在q后p前) for (int p = arr[0].next; p != 0; p = arr[p].next) { // 作为单链表,只能从前向后找(用双向链表可避免) if (arr[p].key > arr[i].key) // 这是从前向后找的缺陷:到相同的,还得继续向后(而直接插入排序是从后向前找的) break; q = p; } arr[i].next = arr[q].next; // p为0时亦然 arr[q].next = i; } } int main(void) { freopen("in.txt", "r", stdin); Record a[LEN + 1] = {0}; a[0].next = 1; //<span style="white-space:pre"> </span>0号单元作为头结点,指针域注意初始化 for (int i = 1; i <= LEN; ++i) cin >> a[i].key >> a[i].otherinfo; LinkListInsertSort(a, LEN); for (int p = a[0].next; p != 0 ; p = a[p].next) cout << a[p].key << '\t' << a[p].otherinfo << endl; return 0; } /* in.txt: 49 1 38 0 65 0 97 0 76 0 13 0 27 0 49 2 out: 13 0 27 0 38 0 49 1 49 2 65 0 76 0 97 0 */
查看全文
相关阅读:
webpack-cli解决办法
说说DBA职责和目标
h5做的app和原生app的区别
安装windows系统的installutil
简化委托调用
DirectShow .Net 实现视频
DirectShowNet 使用摄像头录像+录音
DirectShowLib directshownet 视频
中华人民共和国网络安全法
C#+ html 实现类似QQ聊天界面的气泡效果
原文地址:https://www.cnblogs.com/jjtx/p/2533468.html
最新文章
更改主机名脚本
获取IP地址bash[转载]
IIS7禁止后台访问
深入理解JavaScript系列(13):This? Yes,this!
深入理解JavaScript系列(12):变量对象(Variable Object)
深入理解JavaScript系列(11):执行上下文(Execution Contexts)
深入理解JavaScript系列(10):JavaScript核心(晋级高手必读篇)
深入理解JavaScript系列(9):根本没有“JSON对象”这回事!
深入理解JavaScript系列(8):S.O.L.I.D五大原则之里氏替换原则LSP
深入理解JavaScript系列(7):S.O.L.I.D五大原则之开闭原则OCP
热门文章
深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP
深入理解JavaScript系列(5):强大的原型和原型链
深入理解JavaScript系列(4):立即调用的函数表达式
记录一次Git问题及其解决方案
Windows平台JxCore打包
Nginx之动静分离
Linux关于scp命令
Linux关于压缩和解压缩实例
如何指定安装webpack
webpack执行命令失败之解决办法
Copyright © 2011-2022 走看看