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 */
查看全文
相关阅读:
http协议学习系列
git常用命令大全
git常用命令与常见面试题总结
MyBatis框架及原理分析
Mybatis常见面试题总结
java实现克隆的三种(很最全面)
java中equals和==之间的区别?clone方法的作用,及其为什么要使用clone方法?如何使用clone复制对象?以及深克隆浅克隆
ThreadLocal的简单使用及实现的原理
Java 最常见的 208 道面试题
TCP流量控制
原文地址:https://www.cnblogs.com/jjtx/p/2533468.html
最新文章
thinkphp5.1 中间件是什么,有什么用
搞懂Redis复制原理
分布式锁
迄今为止把同步/异步/阻塞/非阻塞/BIO/NIO/AIO讲的这么清楚的好文章(快快珍藏)
进程、线程、协程、例程、过程的区别是什么?
sql的编写需要注意优化
Foreach用法
【NOIP】提高组2013 货车运输
【NOIP】普及组2010 三国游戏
Swagger使用总结(十九)
热门文章
使用springfox+swagger2书写API文档(十八)
Springfox与swagger的整合使用(十七)
spring boot 使用velocity模板(十六)
Spring Boot 集成 FreeMarker 详解案例(十五)
Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法
SpringMVC工作原理
Spring IOC 和 DI原理 (面试必备)
Java虚拟机(JVM)你只要看这一篇就够了!
面试求职:数据库常见面试题(数据库优化思路)
太厉害了,终于有人能把TCP/IP协议讲的明明白白了!
Copyright © 2011-2022 走看看