插入排序-----数据结构

数据结构-折半插入排序 #include #include #include #define MAXSIZE 20 typedef int KeyType; typedef char InfoType; //结构体定义 typedef struct { KeyType key; InfoType otherinfo; }RedType; typedef struct { RedType r 阅读详情

插入排序

设计思路:

        1.判断数组元素个数,如果数据元素个数小于等于1,无需进行排序,直接返回;

        2.若数组元素个数大于1,设置边界下标值bound = 1,将整个数组分为俩个数组部分,一部分为有序数组,另一部分为无序数组;

        3.再定义一个下标值cur = bound,将下标为cur的所指数组元素与有序数组的元素进行比较,在数组中找到合适的位置;

        4.首先将下标为cur的元素使用将有序数组中大于bound_value进行保存,然后将大于bound_value的元素从后向前依次向后移动一个元素,最后将bound_value插入合适的位置,并将bound++进入下一轮的循环;

        5.当bound等于数组元素个数值size时,循环结束;

        6.此时的数组就是一个有序数组。

插入函数程序代码:

//插入函数
void InsertSort(int array[],size_t size){
    if(array == NULL){
        //非法输入
        return;
    }
    if(size <= 1){
        //无需排序
        return;
    }
    size_t bount = 1;
    for(;bount<size;++bount){
        int bount_value = array[bount];
        size_t cur = bount;
        for(;cur >0;--cur){
            if(array[cur-1] > bount_value){
                array[cur] = array[cur - 1];
            }else{
                //找到量合适的位置
                break;
            }
        }
        array[cur] = bount_value;
    }
    return;
}

测试用例程序代码:

void TestInsertSort(){
    int array[] = {1,3,2,6,8};
    size_t size = sizeof(array)/sizeof(array[0]);
    int i = 0;
    for(;i<size;++i){
        printf("%d ",array[i]);
    }
    printf("\n");
    InsertSort(array,size);
    i = 0;
    for(;i<size;++i){
        printf("%d ",array[i]);
    }
    printf("\n");
}
//主函数
int main(){
    TestInsertSort();
    return 0;
}

测试结果显示:



数据结构与算法之插入排序优化 前言 欢迎各位坚持不懈的小伙伴么继续来到我的博客,与我一起探讨程序员必备技能——数据结构与算法。在看这篇博客之前,要先去了解上一篇博客的核心思想的,才能明白优化之所在。 上一篇博客链接地址:数据结构与算法之插入排序_xue_mall的博客-优快云博客 插入排序优化 上一篇中的核心插入排序代码: /** * 插入排序实现主逻辑 * * @param arr 目标数组 */ private static void insertSort(int 阅读详情

相关推荐

数据结构期末复习之插入排序

直接插入排序 折半插入排序 希尔排序

小白不白 531

插入排序

一、基本思想(直接插入排序) 1.把整个区间分为“有序”和“无序”。 2.每次选择无序区间内的第一个元素插入到有序区间的合适位置。 二、性能分析 选择排序是一种稳定性较好的算法。 空间复杂度为O(1)。 数据有序时,时间复杂度最好,O(N); 数据逆序时,时间复杂度最坏,O(N^2); 平均时间复杂度为O(N^2)。 插入排序的初始数据越接近有序,时间效率越高。 三、代码实现 //插入排序,升序...

weixin_44945496的博客 102

Unity开发HoloLens应用:从打包到部署的完整指南

本文为Unity开发者提供了将应用部署到HoloLens设备的完整实战指南。详细讲解了从环境准备、Unity项目配置、生成Visual Studio工程,到最终构建APPX安装包并通过设备门户或Visual Studio进行部署的全流程,帮助开发者高效完成混合现实应用的打包与安装。

red88的博客 1019

数据结构之顺序储存插入

本文内容摘自大话数据结构,以php方式表述。 引用线性表数据插入3.5.2.我以php数组的方式进行实现。 1.数组指定位置的插入,线性表类似。 public function index(){ $array =[1,2,3,4,5,6,7,8,9];//数组 $array_num =count($array);//定义最大值数组 $i =1;//插入的位置 if($i<1 || $i >$array_num){

weixin_42746203的博客 469

Win10 / Win11 启用 Hyper-V 虚拟机详细教程(图文+命令行全攻略)

摘要:本文详细介绍Windows10/11系统启用Hyper-V虚拟化平台的方法,包含专业版/企业版的图形界面和PowerShell命令行操作,以及家庭版通过脚本强制启用的特殊方案。内容涵盖版本支持对比、操作步骤图解、常见问题解答和性能优化建议,帮助开发者快速搭建虚拟化环境进行多系统测试。附微软官方文档参考链接和详细操作示意图。

ElioXu 7077

一文帮你彻底搞懂ARM Debug Interface之SWD(转)

ARM的文档写的已经很好了,但是关于上电以后的第一时间应该怎么操作,依然写的不够清晰,导致我第一次用的时候还是费了一些周折。今天做一个详细的梳理,希望能够帮助到更多的朋友。BTW, ARM_Cortex-M0/3 DesignStart系列会持续更新,只是中间可能会穿插其他知识点。SWD是Serial Wire Debug的简称,翻译成中文是”串行线调试”。

2302_79847220的博客 6861

数据结构一之顺序存储、链式存储、线性表

顺序存储 顺序存储结构是存储结构类型中的一种,该结构是把逻辑上相邻的结点存储在物理位置上相邻的存储单元中,结点之间的逻辑关系由存储单元的邻接关系来体现。简单来说就是,用一段连续的地址存放数据元素,数据间的逻辑关系和物理关系相同。 线性结构的特点就好比一串珠子,其特点是第一个节点只有一个后继,没有前驱,最后一个节点是只有一个前驱,没有后继。而其余的节点只有一个前驱和一个后继。说白了线性表就是一串。下方这个图就是线性表的示例图。中间蓝色的节点前方的是就是改点对应的前驱,后...

Dog语言 4089

树莓派与ROS2

树莓派与ROS2(Robot Operating System 2)的结合使用为机器人开发、自动化控制以及物联网项目提供了强大的平台。

weixin_47695827的博客 1756

【数据结构】插入排序

数据结构中的插入排序 参考代码如下: /* 名称:插入排序 语言:数据结构C语言版 编译环境:VC++ 6.0 日期: 2014-3-26 */ #include #include #include typedef int KeyType; // 定义关键字类型为整型 typedef int InfoType; // 定义其它数据项的类型 // 记录类型 ty

徐刘根的博客 2480

构建高可用CephFS NFS网关:NFS-Ganesha与RADOS集群的深度整合

本文详细介绍了如何构建高可用的CephFS NFS网关,通过NFS-Ganesha与RADOS集群的深度整合,实现高效文件共享。文章涵盖核心架构、部署实战、性能调优及避坑指南,帮助用户解决Ceph客户端兼容性问题,提升存储访问效率。特别强调高可用设计,确保服务稳定性,适用于金融、电商等高要求场景。

weixin_26976635的博客 254

DCMTK编译

dcmtk-3.6.4 使用Unicode?dcmtk的编译选项里没有关于Unicode的选项,OFString的接口里明确使用了OFString(char*)作为构造函数参数,所以,我们可以假设不能够自己擅自在CMakeList.txt里增加一行 add_definitions(-DUNICODE -D_UNICODE) 来强制使用Unicode编码。 亲测:加了之后编译不过 void...

C++ 爱好者 make_it_simple888 1734

数据结构的运算之插入排序

前面分享完数据结构运算的查找后,今天,我们来看看数据结构的运算中的排序问题,排序问题分为很多种,记得前面有一期文章已经有分享过八种比较常见的排序。下面博主会分享几种比较好用的排序。我们一起来看看吧 首先:插入的结点可能存在多种关键信息,所以有一下定义: 第一种排序;插入排序 ...

m0_63932570的博客 191

YOLOv8首发优化:遥感目标检测 | 专为低质量遥感图像设计的边缘 - 高斯聚合(EGA)模块,解决低空间分辨率、传感器噪声、目标模糊、低光照退化和部分遮挡等限制

一种轻量级网络,其创新性地整合了专为低质量遥感图像设计的边缘 - 高斯聚合(EGA)模块。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 492

数据结构,插入排序

【代码】数据结构,插入排序。

wqs12_的博客 173

三峡大学-微机原理与接口技术实验

其中,A15接E1,因此,A15为1,A14A13A12A11A10A9A8A7通过八输入与非门74LS30与E2连接,因此A14A13A12A11A10A9A8A7为11111111,A6A5A4与CBA连接,因此A6A5A4为101,A3A2A1A0不参与译码,因此IO5的地址范围为1111 1111 1101 0000B~1111 1111 1101 1111B,即0FFD0H~0FFDFH。如图1所示,若IO0输出为低电平,则需要E1接高电平,E2接低电平,E3接低电平,CBA的输入为000。

m0_62222486的博客 496

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

数据结构 Java 实现 插入排序

public class inserSort { static void insertSort(List<Integer> list) { int length = list.size(); int j, i, b; for (i = 1; i < length; i++) { b = list.get(i); if (b < list.get(i - 1)) { .

qq_19551571的博客 228

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

数据结构之插入排序

来一曲插播如何,铛铛~~ 这就是插入排序的代码 public class Insertion{ public static void sort(Comparablep[] a){ for(int i = 1; i < a.length; i++){ for(int j = i; j > 0; j--){ if(greater(a[j-1], a[j])){ exch(a, j

Si_kenn的博客 173

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

JAVA数据结构与算法之插入排序

前言 最近面临毕业就业,在复习数据结构与算法,为了更好地掌握,加深印象,所以决定写一些博客来知识复现。 温馨提示:这篇博客可能不适合刚学数据结构的新手。 算法的简单介绍 插入排序(Insertion Sorting)的基本思想是:把 n 个待排序的元素看成为一个有序表和一个无序表,开始时有 序表中只包含一个元素,无序表中包含有 n-1 个元素,排序过程中每次从无序表中取出第一个元素,把它的排 序码依次与有序表元素的排序码进行比较,将它插入到有序表中的适当位置,使之成为新的有序表。 代码实现 package

iCanCode的博客 278

数据结构-插入排序

数据结构-插入排序 - Timcode - 博客园 (cnblogs.com)

唐攀的博客 195

Golang数据结构-插入排序

文章目录一、Golang数据结构-选择排序1.基本介绍二、使用步骤1.编写冒泡排序的函数2.编写主函数测试三、运行结果 一、Golang数据结构-选择排序 1.基本介绍 选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理如下: (1) 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。 (2) 选择排序的主要优点与数据移动有关。如果某个元素位于正确的最..

qq_43498850的博客 600

数据结构-排序-表插入排序

数据结构-排序-表插入排序 1.算法思想 表插入排序的思想: 2.算法复杂度 执行时间: 附加空间: 是否是稳定的排序方法: 3.算法实现 运行截图 欢迎大家关注我的博客:breeziness123 转载说明出处 ...

qq_40731414的博客 2145

数据结构排序系列详解之一 插入排序

复习之余,就将数据结构中关于排序的这块知识点整理了一下,写下来是想与更多的人分享,最关键的是做一备份,为方便以后查阅。 排序 1、概念:      有n个记录的序列{R1,R2,.......,Rn}(此处注意:1,2,n 是下表序列,以下是相同的作用),其相应关键字的序列是{K1,K2,.........,Kn}。通过排序,要求找出当前下标序列1,2,......,n的一种排列p1,p2,

奋斗的PIG 1560

数据结构6 插入排序

数据结构中,相关资料显示,效率低的排序算法共有3种,分别为冒泡排序,选择排序,与插入排序。 在其余的两篇博客中,我分别介绍了冒泡排序与选择排序,接下来介绍最后一种排序: 插入排序。 相关资料显示,在实际开发的过程中,插入排序比其他两种排序更为常用。那么何为插入排序? 那么上面基础的 插入排序代码如下(为了表达一般性,利用函数模板): #include<iostream>...

qq_20880939的博客 177

重拾数据结构~~~插入排序

写自己理解的插入排序代码 之前学习数据结构一直都没有想着去复现经典的算法,只是对思想掌握个大概,一直没有形成系统的知识链路,很容易忘记已经学过的东西,一到了动手操作就得找各种资料复习,于是想重新过一遍基础,坚持~~~ 1.(自己的理解~插入排序):插入排序就是将单个数据插入到有序的序列中,这个有序序列可以是单个数据,这基本形成我的思路 2.代码复现 #include<stdio.h> ...

小小拓哥 186
上一篇: 希尔排序-----数据结构
下一篇: 合并排序-----数据结构
CYD、孤狼
博客等级 码龄9年 24粉丝 129原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值