惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
About on SuperTechFans
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
G
Google Developers Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Latest news
Latest news
I
Intezer
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Threatpost
博客园 - 【当耐特】
S
Schneier on Security
P
Privacy International News Feed
G
GRAHAM CLULEY
T
Tenable Blog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
博客园 - Franky
Engineering at Meta
Engineering at Meta
美团技术团队
S
Secure Thoughts
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More

Blog Feed

浅谈分布式事务#2 - 字节星球 浅谈分布式事务 - 字节星球 聊聊大学里面的奇葩 - 字节星球 MySQL 加锁机制分析与死锁排查 - 字节星球 Golang pprof 案例实战 - 字节星球 Golang 手写一个 Channel - 字节星球 字节星球终于全栈上新! - 字节星球 欢迎试用 TodoList - 字节星球 记一次“说走就走”的成都行 - 字节星球 Docker Desktop修改默认存储路径 - 字节星球 流程中心使用指南 - 字节星球 微分方程小手册 - 字节星球 近期小记 - 字节星球 字节星球(肥柴之家)搬家了! - 字节星球 如何顺利注册ChatGPT? - 字节星球 披着CLion的外衣实则在讲CMake - 字节星球 守望之墓/电子骨灰盒 - 字节星球 Python笔记 第三章 - 字节星球 WePlanet现已发布! - 字节星球 简单选择排序和堆排序 - 字节星球 希尔排序 - 字节星球 快速排序 - 字节星球 Python 笔记 第二章 - 字节星球 Python笔记 第一章 - 字节星球 Web使用HarmonyOS字体的压缩方案 - 字节星球 字节星球关于在评论区等位置展示IP属地的公告 - 字节星球 MATLAB简明教程#1 - 字节星球 解决Qt5无法连接MySQL数据库的问题 - 字节星球 时隔多年,终于摆脱了控制台 - 字节星球 论内卷 - 字节星球 堆排序 - 字节星球 连通块中点的数量 - 字节星球 合并集合(并查集) - 字节星球 Trie字符串统计 - 字节星球 KMP字符串 - 字节星球
插入排序 - 字节星球
2022-08-03 · via Blog Feed

插入排序

2022/8/3 22:08:00admin2526 阅读0 点赞0 评论

最近在全面学习数据结构,常用算法记录:插入排序,基本思想是将待排序的记录按其关键字的大小逐个插入到一个有序序列(通常为左半部分),直到所有记录插入完成,是一种稳定排序。
空间复杂度:O(1)O(1)
平均时间复杂度:O(n2)O(n^2)

CPP

#include <iostream>

using namespace std;

//直接插入排序(含哨兵)优点:不用判断j>=0,哨兵即为循环结束标志
void insertSort_1(int arr[], int n);
//直接插入排序(不含哨兵)
void insertSort_2(int arr[], int n);
//折半(二分)插入排序 对直接插入排序的优化
void insertSort_3(int arr[], int n);

int main()
{
    int arr[] = {-1, 5, 7, 12, 6, 2, 0, 8, 15, 1, 11}, arr_2[] = {-1, 5, 7, 12, 6, 2, 0, 8, 15, 1, 11}, arr_normal[] = {5, 7, 12, 6, 2, 0, 8, 15, 1, 11};
    int length = (int)(sizeof(arr) / sizeof(int));  //数组长度
    insertSort_1(arr, length);
    insertSort_2(arr_normal, length - 1);
    for (int i = 1; i < length; i++)
        cout << arr[i] << " ";
    cout << endl;
    for(auto item:arr_normal)
        cout << item << " ";
    cout << endl;
    insertSort_3(arr_2, length);
    for (int i = 1; i < length; i++)
        cout << arr_2[i] << " ";
    return 0;
}

void insertSort_1(int arr[],int n)
{
    int i, j;
    for (i = 2; i < n; i++)     //从第二个元素开始,arr[0]为哨兵
    {
        if(arr[i] < arr[i - 1])     //arr[i - 1]为上一个有序序列中的最大元素
        {
            arr[0] = arr[i];
            for (j = i - 1; arr[0] < arr[j]; --j)  //当前元素若小于或等于哨兵元素,则停止移动,哨兵插入该位置后
                arr[j + 1] = arr[j];    //往后移,为待插入元素腾出空位
            arr[j + 1] = arr[0];    //将哨兵插入
        }
    }
}

void insertSort_2(int arr[],int n)
{
    int i, j, temp;
    for (i = 1; i < n; i++)     //从第二个元素开始,arr[0]为哨兵
    {
        if(arr[i] < arr[i - 1])     //arr[i - 1]为上一个有序序列中的最大元素
        {
            temp = arr[i];  //临时存储待插入元素
            for (j = i - 1; j >= 0 && temp < arr[j]; --j)  //当前元素若小于或等于待插入元素,则停止移动,待插入元素插入该位置后
                arr[j + 1] = arr[j];    //往后移,为待插入元素腾出空位
            arr[j + 1] = temp;    //待插入元素插入
        }
    }
}

void insertSort_3(int arr[], int n)
{
    int i, j, low, high, mid;
    for (i = 2; i < n; i++)
    {
        arr[0] = arr[i];    //待插入元素存入哨兵节点
        low = 1, high = i - 1;  //折半查找的区域
        while(low <= high){
            mid = (low + high) / 2;
            if(arr[mid] > arr[0])
                high = mid - 1;     //查找左半部分
            else
                low = high + 1;     //查找右半部分,同时当arr[mid]==arr[0]时,继续在mid右方查找插入位置,保证算法稳定性
        }
        for (j = i - 1; j >= high + 1; j--)     //循环大于哨兵的所有元素,均往后移动一位
            arr[j + 1] = arr[j];    //往后移,为待插入元素腾出空位
        arr[high + 1] = arr[0];    //待插入元素插入
    }
}

image.png

附件下载