【模板】选择排序

选择排序实现链表排序与模板函数解析 本文介绍了如何使用选择排序算法对整数链表进行排序,并通过C++代码展示了具体的实现过程。此外,文章还深入探讨了模板函数的相关知识点,包括模板函数的目的、模板前缀的作用、参数匹配规则、实例化时机以及模板类实现中的注意事项。同时,还涉及了迭代器的使用、节点操作函数、数据类型泛化方法等内容,适用于C++编程中对数据结构与泛型编程的理解与应用。 阅读详情

选择排序基本思想

简单选择排序的第i趟,总是从(elem[i],elem[i+1],...,elem[n-1])的未排序元素中选择最小的元素加入,然后只通过一次交换将当前最小的元素放在正确的位置。

最好、最坏、平均时间复杂度

都是o(n^2),因为无论如何都要进行的循环次数是:

\sum_{i=0}^{n-2}_(n-1-i) = n(n-1)/2

稳定性

选择排序是不稳定的!如序列[5 8 5 2 9], 我们知道第一次选择第1个元素5会和2交换,那么原序列中2个5的相对前后顺序就被破坏了,所以选择排序不是一个稳定的排序算法。

特性

1.思路简单,性能稳定,无论什么数据进去都是o(n^2)的时间复杂度。

2.不占用额外的内存空间。

//选择排序 
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
void swap(int a[],int elem1,int elem2)
{
	int tmp = a[elem1];
	a[elem1] = a[elem2];
	a[elem2] = tmp;
}
void selectSort(int a[],int n)//选择排序:总是选择当前最小的元素加入序列 
{
	for(int i=0;i<n-1;i++)//依次遍历,寻找当前最小的元素 
	{
		int lowIndex = i;//记录a[i,...,n-1]中最小的元素下标,为最后交换准备 
		for(int j=i+1;j<n;j++)
		{
			if(a[j]<a[lowIndex]) lowIndex = j; //更新 
		}
		swap(a,lowIndex,i);//交换 
	}
 } 
 int main(void)
{
	int n = 10;
	int arr[10] = {0};
	for(int i=0;i<n;i++)
	{
		arr[i]=rand()%100;
	}
	for(int i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl; 
	selectSort(arr,n);
	for(int i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	return 0;
} 

 

选择排序算法的c++模板 选择排序算法的c++模板,适合初学数据结构与算法的,有详细的算法思路 立即下载

相关推荐

门控时钟(clock gating)的DFT设计

在数字IC芯片中,时钟树的功耗占比可能高达30%,因此一般会采取门控时钟的方式来降低该部分的功耗。所谓门控时钟,就是在芯片实际工作过程中,有些信号或者功能并不需要一直开启,那么就可以在它们不用的时候将其时钟信号关闭。这样一来信号不再翻转,从而能够有效减少动态功耗。 目前门控时钟都采用集成门控时钟单元(ICG,integrated clock gating cell),其结构如下。由一个latch和一个与门(也有的是或门)组成,可以过滤掉EN信号中的毛刺。一般的工艺库中都会有专门的ICG以供调用。 插入IC

weixin_44746697的博客 3888

【选择排序模板】

选择排序(Selection Sort)是一种简单直观的排序算法,它每次从待排序序列中选择最小(或最大)的元素,放到已排序序列的末尾(或开头),直到所有元素都被排序。在待排序序列中,找到最小(或最大)的元素。将最小(或最大)的元素与待排序序列的第一个元素交换位置,将该元素视为已排序部分的末尾(或开头)。在剩余的待排序序列中,重复步骤1和步骤2,直到所有元素都被排序。选择排序的时间复杂度为O(n^2),其中n是待排序序列的长度。

学习愚公的博客 297

基于555定时器的D类功放设计:从PWM原理到无反馈电路实践

D类功放(数字功放)是一种高效率的音频放大技术,其核心原理是脉冲宽度调制(PWM)。它将输入的模拟音频信号转换为一系列高速开关的脉冲,通过控制功率开关管(如MOSFET)的通断,再经低通滤波器还原放大后的音频。相比传统AB类功放,其转换效率通常超过90%,显著降低了热能损耗。这项技术在追求高能效和小型化的现代音频设备中价值凸显,广泛应用于有源音箱、汽车音响和便携式设备。本文聚焦于一种独特的实现方案:以经典的555定时器芯片为核心,构建一个无整体负反馈的D类功放。通过剖析其PWM调制器、FET电压-电流转换级

weixin_30162121的博客 236

选择排序(c语言)

中文名:选择排序英文名:Selection Sort、时间复杂度:O(n^2)稳定性:不稳定的排序算法原理:第一次从待排序的元素中选出最小(或最大)的一个元素a,记录其位置的下标,然后将剩下的元素依次与元素a相比较,直到找到剩下的元素中最小的元素b(且元素b比元素a小),二者交换位置(第一次循环结束),以此类推,直到全部待排序的数据元素的个数为零。

m0_64681789的博客 2万+

基于大疆TSDK与Pix4D的热红外数据工程化处理:从RJPG到温度正射影像的完整工作流

本文详细介绍了基于大疆TSDK与Pix4D的热红外数据工程化处理完整工作流。通过Python脚本自动化解析RJPG格式的热红外图像,提取并转换其中的原始辐射数据为包含真实温度信息的TIFF文件,再结合Pix4Dmapper进行批量拼接,最终生成可直接用于GIS分析的温度正射影像图。该流程解决了手动处理效率低下、参数校准复杂等难题,实现了从原始数据到定量分析成果的高效、准确、可重复生产。

weixin_29161785的博客 379

OpenWrt路由开启DDNS+端口转发进行外网访问

OpenWrt路由开启DDNS+端口转发进行外网访问(LuCI界面实现)--开篇一、准备工作二、开机!三、连接--结尾 –开篇 本篇适用于一切装有OpenWrt系统的路由器/软路由,只要你保证有WAN接口和一个光猫,甚至你不需要有LAN接口(当然前提是你能连接路由器的WIFI进后台,不然你只能通过LAN进后台咯)。所有的配置过程都是基于LuCI界面来的,当然大神们一般都通过SSH登陆路由使用命令配置,但我也不会啊,只能给个有GUI界面的教程了。 一、准备工作 一个装有OpenWrt系统的路由器/软路由并

秋之颂的博客 9万+

排序 - 选择排序(Selection sort)

它的基本思想是: 首先在未排序的数列中选出最小(or最大)元素,然后将其存放到数列的起始位置;接着,再从剩余未排序的元素中继续选出最小(or最大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

埃泽漫笔 794

将赛灵思 SDK 工程移植到 Vitis 的分步指南

注:本文转自赛灵思中文社区论坛,源文链接在此。本文原作者为XILINX工程师。 以下为个人译文,仅供参考,如有疏漏之处,还请不吝赐教。 您是赛灵思软件开发套件 (SDK) 用户吗? 您是否正在计划将自己的工程移植到 Vitis 软件平台? 那么本篇工程移植分步指南将能祝您一臂之力。 从 2019.2 版开始,赛灵思 SDK 开发环境已统一整合到全功能一体化的 Vitis™ 统一软件平台中。 尊敬的 SDK 用户,您只需轻点几下鼠标即可体验这一功能丰富的工具! 马上开始将工程从赛灵思 S

weixin_45825101的博客 1733

三种常见的排序模板

如果要对具有n个元素的数组a进行排序,初始状态时,可以认为已排序序列为a[0],待排序序列为a[1]~a[n-1],排序过程如图所示。选择排序的基本思想是:每次从待排序序列中选择一个关键字最小的元素,(当需要按关键字升序排列时),顺序排在已排序序列的最后,直至全部排完。在选择类排序方法中,根据从待排序序列中选择元素的方法不同,又分为不同的选择排序方法,其中最简单的是通过顺序比较找出待排序序列中的最小元素,称为简单选择排序,如图所示。(1)首先将第一个元素与第二个元素进行比较,若为逆序,则将两元素交换。

2301_80708596的博客 1400

Unreal5从入门到精通之Landscape地形系统

虚幻引擎 5 (UE5) 能够利用其强大的地形编辑工具套件创建大规模的地形世界。使用地形工具可以创建身临其境的户外地形,这些地形经过优化,可在多种不同设备上保持可播放的帧速率。

TxNet 4169

(九)DFI接口时序

文章目录一、DFI Interface 一、DFI Interface   DFI接口是连接DDR Controller与DDR_PHY之间的通用接口,其信号组如下表:       

Mr.翟的博客 1万+

c++模板,选择排序,字符数组,字符串

模板不可以直接使用比如一寸照,不能直接不p照片就用。不是万能的,不如不能把小狗的照片p到刚刚一寸照上。

周周的博客 1231

【WorkBuddy专栏53】我的WB为什么变聪明了——SOUL与USER配置管理实战(上)

【WorkBuddy专栏53】我的WB为什么变聪明了——SOUL与USER配置管理实战(上)

AI人人懂 257

C++选择排序模板

template void Swap(T& a, T& b) { T c = a; a = b; b = c; } template void SelectionSort(T* arr, int n) //选择排序 { for(int i = 0;

恨天低 876

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

选择排序模板

选择排序模板

weixin_39104847的博客 492

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

c++ java 选择排序,C++模板元编程实现选择排序

前言模板在C++一直是比较神秘的存在。 STL 和 Boost 中都有大量运用模板,但是对于普通的程序员来说,模板仅限于使用。在一般的编程中,很少会有需要自己定义模板的情况。但是作为一个有理想的程序员,模板是一个绕不过去的坎。由于C++标准的不断改进,模板的能力越来越强,使用范围也越来越广。在C++11中,模板增加了 constexpr ,可变模板参数,回返类型后置的函数声明扩展了模板的能力;增加...

weixin_29369363的博客 165

选择排序(模板)

基本思想: 每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完 。 直接选择排序的特性总结: 直接选择排序思考非常好理解,但是效率不是很好。实际中很少使用 时间复杂度:O(N^2) 空间复杂度:O(1) 稳定性:不稳定 #include <vector> //升序(小于) template<class T> b...

晶凌玄夜的博客 519

用模板写选择排序-链表

今天,我们一起用mo'b

尹成的技术博客 2246

c++模板选择排序

#ifndef _BUBBLESORT_H_//bubblesort.h #define _BUBBLESORT_H_ #define MAX_SIZE 100 #include using namespace std; //template template class DataList { private: ElemType data[MAX_SIZE]; int size; void

leafinsnowfield的专栏 691

排序模板 选择法排序

#include"iostream" using namespace std;   template   void Sort(T *p,int n)   {       int i,j,index;       T t,min;       for(i=0;i     {           min=*(p+i);           index=i;           f

duchunshui的博客 518

C++用模板的方式进行选择排序

测试 char 数组、int数组。使用通用 对数组进行排序。//排序后进行数组打印。//char数组测试。

weixin_52513174的博客 138

利用模板进行数组的选择排序

用模板函数进行不同类型数据的选择排序

m0_64727698的博客 803

模板实现选择排序(c++代码实现)

模板实现选择排序 代码如下: #include<iostream> using namespace std; template<class T> void myswap(T &a,T &b) { T temp = a; a = b; b = temp; } template<class T> void sort(T arr[],int len) { for (int i = 0; i < len; i++) { int max = i;

m0_53175693的博客 695

利用c++模板实现选择排序

/* SelectSort.hpp */ #ifndef _SELECT_SORT_H_ #define _SELECT_SORT_H_ template bool SelectSort(T *pInput, int nLen) { int i = 0; int j = 0; int nMin = 0; T tTemp; if(!pInput) { return false

zhuwenfeng的专栏 754

函数模板实现选择排序

#include<iostream> using namespace std; #include<string> //选择排序 template<typename T> void mysort(T arr[], int len) { for (int i = 0; i < len; i++) { int max = i; //认定最大值下标 for (int j = i + 1; j < len; j++) { if (arr

qq_51419689的博客 865
上一篇: 【模板】快速排序及寻找区间第k大
下一篇: 【模板】归并排序
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址