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 */
查看全文
相关阅读:
八张图读懂未来“互联网+”的六大趋势
跑一段代码遍历所有汉字
PHP业务逻辑层和数据访问层设计
漫谈社区PHP 业务开发
以Apache服务器、php语言为例 详解动态网站的访问过程
sublime text
《产品经理的20堂必修课》
检测文件是否有bom头
利用开源框架Volley来下载文本和图片。
往SD卡中写文件的方法。
原文地址:https://www.cnblogs.com/jjtx/p/2533468.html
最新文章
「暑期训练」「基础DP」FATE(HDU-2159)
「暑期训练」「基础DP」 Monkey and Banana (HDU-1069)
「暑期训练」「基础DP」 Piggy-Bank (HDU-1114)
「暑期训练」「基础DP」 Common Subsequence (POJ-1458)
「暑期训练」「Brute Force」 Bitonix' Patrol (CFR134D1D)
「暑期训练」「Brute Force」 Multiplication Table (CFR256D2D)
「暑期训练」「Brute Force」 Optimal Point on a Line (Educational Codeforces Round 16, B)
「日常训练」「小专题·图论」Domino Effect(1-5)
Rabbitmq(6) 主题模式
Rabbitmq(5) 路由模式
热门文章
Rabbitmq(4) 订阅模式
Rabbitmq(3) work queues
RabbitMq(2) 简单消息队列
RabbitMq (1)
热部署
spring cloud之路(1)
pom
CKEditor 5
前端收集
舶来干货资源
Copyright © 2011-2022 走看看