插入排序

C++插入排序 文章目录前言一、插入排序的基础知识点1.插入排序的排序原理2.插入排序的所属类别3.插入排序的算法复杂度二、插入排序动态图三、代码总结 前言 今天来和大家分享插入排序。 一、插入排序的基础知识点 1.插入排序的排序原理 用未排序序列中的首个元素a,与已排序元素b从后向前逐个对比,若a>b则a插入b的后一位,若a<b,则a、b位置互换。 2.插入排序的所属类别 插入排序属于“比较类排序” 3.插入排序的算法复杂度 最坏的情况为与所求序列相反的序列,所以需要依次操作 1、2、3…(n-1)次. 阅读详情

//InsertSort.h

#ifndef _INSERTSORT_H_
#define _INSERTSORT_H_

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define SIZE	15

typedef int Element;
typedef Element ListType[SIZE + 1];	//第0个元素用作哨兵或是辅助空间

void CreateRandom(ListType List, int n);
void print(ListType List, int n);

//直接插入排序				
void InsertSort(ListType List, int n);

//二路插入排序				
void BinWayInsertSort(ListType List, int n);

//希尔排序
void ShellSort(ListType List, int n);




#endif //_INSERTSORT_H_

//InsertSort.c

#include "InsertSort.h"

void CreateRandom(ListType List, int n)
{
	int i = 0;
	srand((unsigned)time(NULL));

	for (i = 1; i < n; ++i)
	{
		List[i] = rand() % 0x7F;
	}
}

void print(ListType List, int n)
{
	int i = 0;
	for (i = 1; i < n; ++i)
	{
		printf("%d\t", List[i]);
	}
	printf("\n");
}

/*******************************************************
使用新元素与每个有序数组中的元素对比,移动所有比新元素大的元
素,插入新元素
*******************************************************/
//如果不增加哨兵位,为for (j = i - 1; j >= 0 && List[j] > Temp; j--),这样写即要判断越界情况又要判断大小,增加了哨兵位则无需注意越界问题 
void InsertSort(ListType List, int n)
{
	int i = 0, j = 0;
	for (i = 2; i < n; ++i)								//第0位是哨兵位,不参与排序
	{
		if( List[i] < List[i - 1] )						//往有序数组[0 .... i - 1]中插入元素[i]
		{
			List[0] = List[i];							//用哨兵位保存待插元素
			for (j = i - 1; List[j] > List[0]; j--)		//将有序数组[0 ..... i - 1]中比[i]大的全部元素向右移动为[i]元素留出位置
			{
				List[j + 1] = List[j];
			}
			List[j + 1] = List[0];						//将[i]元素插入合适位置
		}
	}
	List[0] = 0;										//将哨兵赋为初始值
}

/*******************************************************
使用一个与原数组同样大小的数组作为辅助空间,辅助空间被视为循
环数组,使用一个头指针与尾指针指向辅助数组中最小于最大的元素,
使用新元素分别于头尾指针指向的值比较,如果新元素大于或等于最
大元素,直接将新元素放在最大元素逻辑上的后方,如果新元素小于
最小的元素,则直接放入最小元素逻辑上的前方,当新元素不小于最
小元素且小于最大元素时,将大于新元素的所有元素逻辑上后移为新
元素留出位置,将新元素插入不大于新元素的元素的位置之后
*******************************************************/
//2路插入排序相对于直接插入排序减少了移动的次数,但是没有减少比较次数
void BinWayInsertSort(ListType List, int n)
{
	int i = 0, j = 0, head = 0, tail = 0;
	ListType Temp = { 0 };

	Temp[0] = List[1];									//第0位在此处未使用,在其他排序时第0位作为了辅助位或哨兵位,2路排序使用了额外的辅助空间,故不使用第0位
	head = tail = 0;
	for (i = 2; i < n; ++i)								//第1位已经移动过去。从第2位开始遍历,向循环数组中插入待插元素
	{
		if( List[i] >= Temp[tail] )						//待插元素比有序数组[0......tail]中最大的一个元素[tail]还大或相等,直接放在[tail]后面,稳定
		{
			Temp[++tail] = List[i];						//由于Temp从第个位置开始往后赋值,空间足够,所以往后不会有任何越界或者覆盖[head]值的风险
		}
		else if( List[i] < Temp[head] )					//待插元素比有序数组[0......tail]中最小的一个元素[head]还小,直接放在[head]前面,没有判断相等情况,为了稳定
		{
			head = (head - 1 + (n - 1)) % (n - 1);		//不能直接-1,直接减存在越界风险,又由于数组大小个数包含了辅助位,即比正常大小多了一个,没有使用多的一个,故减去(n - 1)	
			Temp[head] = List[i];
		}
		else											//待插元素值大小位于有序数组[0......tail]中间位置或与[head]相等
		{
			for (j = tail; List[i] < Temp[j]; j = (j-1 + (n-1)) % (n-1))		//将有序数组[0......tail]中比[i]大的元素全部后移(相等的未曾移动,稳定)为[i]留出位置
			{
				Temp[(j + 1) % ( n - 1 )] = Temp[j];	//移动数据为[i]留出位置
			}
			tail++;										//此情况下有数据的移动,tail指针也不要忘了
			Temp[(j + 1) % (n - 1)] = List[i];
		}
	}
	for(i = 1, j = head;  j != tail; ++i, j = ((j+1) % (n-1)))		//赋值回待排序的数组
	{
		List[i] = Temp[j];
	}
}


/*******************************************************
希尔排序是在直接插入的基础上改进的,希尔排序将所有元素以增量
作为分组(增量最后必须为1),在每组中以增量为步长移动组中所有比
待插元素大的元素以留出位置,最后将新元素插入
*******************************************************/
void ShellSort(ListType List, int n)
{
	int Step = 0, i = 0, j = 0;
	for (Step = n - 1; Step >= 1; Step /= 2)			//Step为增量,由于第0个元素不参与排序,故将元素个数减少1再计算增量
	{
		for (i = Step + 1; i < n; ++i)					//从第一组增量开始遍历完每一组增量	, 由于0位是辅助位,所以以第一位所在组作为第一组	
		{
			if( List[i] < List[i - Step] )				//比较一组增量上的两个元素
			{
				List[0] = List[i];						//保存元素[i]
				for (j = i - Step; j > 0 && List[j] > List[0]; j -= Step)	//组中以增量为步长移动组中所有比待插元素大的元素以留出位置
				{
					List[j + Step] = List[j];
				}
				List[j + Step] = List[0];				//在合适的位置插入
			}
		}
	}
}



//mian.c

#include "InsertSort.h"

int main(int argc, char **argv)
{
	ListType List = { 0 };
	int n = sizeof(List) / sizeof(Element);

	CreateRandom(List, n);
	printf("排序前\n");
	print(List, n);

//	InsertSort(List, n);
//	BinWayInsertSort(List, n);
	ShellSort(List, n);
	printf("排序后\n");
	print(List, n);

	system("pause");
	return 0;
}



深入浅出卡尔曼滤波器算法 深入浅出讲解卡尔曼滤波器的工作原理及其公式推导,讨论卡尔曼滤波器的核心公式所代表的意义及个人的理解。 阅读详情

相关推荐

【GD32F407】i2c主机发送,从机中断接收

前言 现在手里负责的一个项目需要用i2c和主芯片进行通信主动发送,中断接收,这就需要支持i2c主机模式和从机模式,先查看原理图 这里我们用到了两条i2c总线进行数据的交替传输,避免在使用一条总线是发送数据和接收数据冲突 初始化i2c控制器 s32_k i2cDevInit(u8 byBusId, TI2cBus *pTI2cBus) { //PCLK = 36000000 BOOL retFlg = FALSE; int I2xSCL = pTI2cBus->byFastMode ?

Enosji的博客 6248

python【2】编写程序,从键盘输入10个数,比较大小,从小到大升序打印

编写程序,从键盘输入10个数,比较大小,从小到大升序打印。

此人很懒 1461

BurpSuite+安卓模拟器双保险:避开真机抓包限制的完整配置指南

本文详细介绍了如何通过BurpSuite与安卓模拟器(如雷电、夜神)协同工作,绕过真机抓包限制的完整配置指南。从环境准备、网络拓扑配置到证书部署和流量分析,提供了一套高效的解决方案,帮助安全测试人员稳定捕获手机数据包,提升测试效率。

Apple的专栏 439

用选择排序法编写c语言,实现从键盘上输入10个数,按从大到小的顺序排序输出。

用选择排序法编写c语言,实现从键盘上输入10个数,按从大到小的顺序排序输出。#include&lt;stdio.h&gt;int main(){ int i,j,a[10],t; printf("请输入十个数:"); for (i = 0; i &lt; 10; i++) scanf("%d",&amp;a[i]); for (i = 0; i &lt; 10; i++) { for (j =...

qq_41973721的博客 5万+

避坑指南:Unity新版InputSystem在移动端的5个常见问题及解决方案

本文针对Unity新版InputSystem在移动端开发中的常见问题,提供了5个关键解决方案,包括虚拟摇杆灵敏度调节、多点触控冲突处理、按钮反馈延迟优化、手势识别与陀螺仪数据融合以及Android/iOS平台差异处理。这些实战经验帮助开发者高效解决触控适配难题,提升移动游戏体验。

soda5的博客 405

ESP32 STA AP模式下,设备连接 ESP的AP 后无法上网,无法访问域名上网(或者Failed to enable NAPT)

ESP32 STA AP模式下,设备连接 ESP的AP 后无法上网,无法访问域名上网(或者Failed to enable NAPT)1.可能是ESP STA模式未成功连接到路由器,确保 STA 成功连接到外部网络。2.检查 STA 和 AP 的网络配置是否正确,避免子网冲突。3.ESP32的NAPT功能未正确启用,确保 menuconfig 中已启用 IP_NAPT。4.DNS服务器未正确配置。

qq_34885669的博客 1540

十大经典排序算法(动图演示)

0、算法概述0.1 算法分类十种常见排序算法可以分为两大类:非线性时间比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此称为非线性时间比较类排序。线性时间非比较类排序:不通过比较来决定元素间的相对次序,它可以突破基于比较排序的时间下界,以线性时间运行,因此称为线性时间非比较类排序。 0.2 算法复杂度0.3 相关概念稳定:如果a原本在b前面,而a=b,排序之...

聆听晨雨的博客 581

自动驾驶关键技术(一):时间敏感网络TSN

时间敏感网络TSN的基本概念、协议族介绍和开发流程。

3085

输入10个整数,用直接插入法对其进行排序;

1、输入10个整数,用直接插入法对其进行排序; #include #defineN10 intmain() { inti,j,t,a[N]; printf("输入十个数:\n"); for(i=0;i scanf("%d",&a[i]); for(i=1;i t=a[i]; for(j=i-1;j>=0&&a[j]>t;j--){ a[j+1

优快云_buyi的博客 6413

YOLOv8 | 代码逐行解析(一) | 项目目录构造分析

Hello,大家好这次给大家带来的不是改进,是整个YOLOv8项目的分析,整个系列大概会更新7-10篇左右的文章,从项目的目录到每一个功能代码的都会进行详细的讲解,同时YOLOv8改进系列也突破了三十篇文章,最后预计本专栏持续更新会在年底更新上百篇的改进教程, 所以大家如果没有订阅专栏可以提前订阅以下。下面开始进行YOLOv8逐行解析的一篇——项目目录构造分析在这里给大家推荐我的YOLOv8改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,。

Snu77的博客 4万+

基于Python的车牌识别系统实现

本文将以基于Python的车牌识别系统实现为方向,介绍车牌识别技术的基本原理、常用算法和方法,并详细讲解如何利用Python语言实现一个完整的车牌识别系统。

商务合作 / 项目定制 / 学习交流。个人vx:lovely_wml 5万+

插入排序以任意顺序输入的10个数

#include<stdio.h> int main() { int a[10]; int i, j, k; printf("输入10个数:"); for (i = 0; i < 10; i++) { scanf("%d", &a[i]); if (i == 0) continue; k = a[i]; j = i - 1; while...

qq_40943335的博客 567

深度学习工具包 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")

插入排序详解

摘要:插入排序是一种非常有意思且比较高效的排序方法,同时插入排序是希尔排序的基础,现在我们来看看插入排序。

云杉木屋 4万+

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

插入排序-简单插入排序和二分插入排序

插入排序: 插入排序(英语:Insertion Sort)是一种简单直观的排序算法。 它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。 基本思想 每一步都是将 待排序的一个数据,插入到一个已经有序的数据序列里面。直到所有的数据全部插入为止。 类似于打牌【斗地主】,每摸一张就按照顺序放到位置。每

Riptide 503

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

表插入排序

表插入排序

Keep_Trying_Go的博客 2237

JS实现插入排序

一,插入排序1,算法简介插入排序的工作原理就是将未排序数据,对已排序数据序列从后向前扫描,找到对应的位置并插入。插入排序通常采用占位的形式,空间复杂度为O(1),因此,在从后向前扫描的过程中,需要反复的把已排序的元素逐步向后挪位,为新插入元素提供插入的位置。2,算法描述1)从第一个元素开始,该元素可以被认为已经被排序2)取出下一个元素,在已经排好序的序列中从后往前扫描3)直到找到小于或者等于该元素...

bangbanggangan的博客 1万+

插入排序与折半插入排序比较

插入排序是一种原地排序算法,即它的空间复杂度为o(1),原始插入排序的最坏(即原始序列的顺序刚好和我们所希望的顺序相反)和平均时间复杂度均为o(n*n),而在最好的情况下(即原始序列已经是按照我们所希望的顺序排好的序列)为o(n)。 下面的例子展示原始插入排序的整个过程:整个序列存放在数组中,按照从小到大的顺序进行排列。 8 2 4 9 3 6.//原始序列 从第二个元素开始,每个元素依次与

mayo2012的专栏 1251

插入排序-2-路插入排序

2-路插入排序    一、定义     2-路插入排序是在折半插入排序的基础上再改进,其目的是减少排序过程中移动记录的次数,但为此要付出n个记录的辅助空间。 二、具体实现     另设一个同类型的数组d,先将list[1]复制给d[1],并将d[1]看成是排好序的序列中处于中间位置的记录,然后从list中第二个记录起依次插入到d[1]之前或之后的有序序列中。先将待插入记录和d[1]进行比较,若大于d[1],则

对自己笑 1466

排序之二分插入排序

作为一个稳定的排序算法, 插入排序很重要,大多数程序员都可以很轻松的写出插入排序! 先看一下插入排序的代码: void sort_insert(int a[],int n) { int i,j; i=1; while(i<n) { int x = a[i]; j=i-1; while(j>=0 && a[j]>x) { a[j+1]=a[j];

Ben的专栏 4686

简单插入排序,折半插入排序和2路插入排序 c源码

以下三种插入排序时间复杂度均为O(n^2) 简单插入排序,简单直接。假定数组有序,插入 i, 从后往前遍历找到适合位置 j,移动 j +1 ~ i -1往后一位,插入i到j中。 void insertSort(int *arr, int numsSize) { int i, j, k, v; for (i = 1; i < numsSize; i++) { for (j = ...

fuyuande的博客 331

插入排序之二路插入排序

可以说二路插入排序是对折半插入排序的进一步改进,目的就是为了减少折半插入排序记录移动的次数, 但是二路插入排序需要借助n个记录的辅助空间,也就是说他的空间复杂度为O(n); 初始关键字序列   51     33          62           96 87 17 28 51 i=1 [51] first      final i=2

tongnianhani的博客 1302

插入排序及优化

插入排序是简单排序的一种,也是基于“减治法”思想的一种算法,减治法有3种变形: 减去一个常量; 减去一个常量因子; 减去的规模是可变的。 插入排序算法的时间复杂度和冒泡、选择排序算法一样也是o(n²),常见的基于插入排序算法思想的排序算法有: 直接插入排序算法; 折半插入排序算法; 希尔排序算法。(查看) 插入的方法一共有三种:

DR的专栏 3416

插入排序解析

插入排序是一种经典的简单排序算法,其核心思想是通过构建有序序列来逐步完成整体排序。该算法将待排序数组分为已排序和未排序两部分,每次从未排序部分取出第一个元素,在已排序部分从后向前扫描,找到相应位置并插入。这个过程类似于玩扑克牌时整理手中牌序的方式,具有直观易懂的特点。从算法特性来看,插入排序在最优情况下(输入数组已基本有序)时间复杂度可达到O(n),这使其对部分有序数据表现出良好的性能。然而在最坏情况下(完全逆序输入),时间复杂度会退化到O(n²)。不过由于其内层循环可以在找到插入位置时提前终止,实际运

2401_88413914的博客 772
上一篇: 白话经典算法系列之三 希尔排序的实现
下一篇: 交换排序
早起的虫儿灬
博客等级 码龄10年 54粉丝 114原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值