runxinzhi.com
首页
百度搜索
插入排序之表插入
表插入
时间复杂度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 */
相关阅读:
mysql这个垃圾迁移数据费劲半天
https请求,可设置请求格式
mybatis plus 使用
https请求,get post 通用
根据经纬度,查询最近距离
redis与mysql一致性
导入excel
在idea中git怎么使用
spring的传播机制
通过RequestContextHolder获取HttpServletRequest
原文地址:https://www.cnblogs.com/jjtx/p/2533468.html
最新文章
oracle发送HTTP请求参考例子
哪位大侠赐教,oracle b-tree索引中,键值的作用是什么,还有,如何判断一个索引有几层呢,多谢
如何使用Oracle FND_LOAD工具在不同Instance间同步数据(Example)
Oracle EBS 基于Host(主机文件)并发程序的开发
EBS API及接口EBS API及接口
oracleEBS常用表视图会计分录mtl_serial_numbers总账
MTL_SERIAL_NUMBERS.CURRENT_STATUS
Springboot项目中Mybatis升级Mybatis-plus依赖冲突问题
JAVA 增删改查接口命名规范
yum下载rpm安装包 再离线安装
热门文章
几种限流算法的go语言实现
MYSQL IN 一定走索引吗?
Go语言syncMap LoadOrStore
微服务—go中使用gRPC
windows 安装protocol buffers
Golang 语言面向对象编程(下)
Golang 语言面向对象编程(上)
比Xshel更好用的 FinalShell
go-二叉搜索树
go 查询连休天数的员工信息
Copyright © 2020-2023
润新知