数组的排序,加入数组中相邻元素的置换

2014-09-1 相邻数对(排序) 给定 n 个不同的整数,问这些数中有多少对整数,它们的值正好相差 1。 输出格式 输入的第一行包含一个整数 n,表示给定整数的个数。 第二行包含所给定的 n 个整数。 输出格式 输出一个整数,表示值正好相差 1 的数对的个数。 数据范围 1≤n≤1000, 给定的整数为不超过 10000 的非负整数。 输入样例: 6 10 2 6 3 7 8 输出样例: 3 样例解释 值正好相差 1 的数对包括 (2,3),(6,7),(7,8)。 C++: /* 先排序 然后遍历一遍判断差值为1 即可 */ #i 阅读详情
/*
按大小排列数组中元素的顺序{2,23,5,6,9,1}
*/
//从小到大排列
//内循环结束一次,最值出现在头角标位上
//选择排序
class ArrayTest2
{
	public static void selectsort(int[] arr)
	{
		for(int x=0;x<arr.length-1;x++)
		{
			for(int y=x+1;y<arr.length;y++)
			{
				if(arr[y]<arr[x])
				{
					/*
					int temp=arr[x];
					arr[x]=arr[y];
					arr[y]=temp;
					*/
					swap(arr,x,y);//换位置
				}
			}
		}
	}
	//打印数组
	public static void printArray(int[] arr)
	{
		System.out.print("[");
		for(int x=0;x<arr.length;x++)
		{
			if(x!=arr.length-1)
				System.out.print(arr[x]+",");
			else
				System.out.println(arr[x]+"]");
		}
	}
	
	public static void main(String[] args)
	{
		int[] arr={2,23,5,6,9,1};
		//printArray(arr);
		//selectsort(arr);//选择排序
		//Arrays.sort(arr);//java中已经定义好的一种排序方式,开发中对数组排序要使用该句代码
		bubblesort(arr);
		printArray(arr);
	}
	
	
	//冒泡排序:相邻的两个元素相互比较,如果符合条件换位
	//第一圈,最值出现在最后位
	
	public static void bubblesort(int[] arr)
	{
		for(int x=0;x<arr.length-1;x++)
		{
			for(int y=0;y<arr.length-x-1;y++)//-x让每一次比较的元素减少。-1避免角标越界
			{
				if(arr[y]>arr[y+1])
				{
					/*
					int temp=arr[y];
					arr[y]=arr[y+1];
					arr[y+1]=temp;
					*/
					swap(arr,y,y+1);
				}
			}
		}
	}
	
	/*
	发现无论什么排序都需要对满足条件的元素进行未知置换
	所以可以把这部分相同的代码提取出来,单独封装成一个函数
	*/
	public static void swap(int[] arr,int a,int b)
	{
		int temp = arr[a];
		arr[a] = arr[b];
		arr[b] = temp;
	}
}

剑指Offer面试题03-找出数组中重复的数字(5种方法) 题目: 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 示例 1: 输入: [2, 3, 1, 0, 2, 5, 3] 输出:2 或 3 限制:2<=n<=100000 题目很好理解,没有什么需要分析的部分.既然n个数字,范围却是0~n-1,肯定有重复的. 一.统计个数 题目是为了找重复的数字,最简单的方法就是把他们出现的次数都统计出来,结果大于1的, 阅读详情

相关推荐

VCL for PHP,Konopka Signature VCL Controls (Raize Components 6.2.3) for Delphi 10.4

The Raize Components suite includes a variety of interface design tools for Delphi programming language and C ++ Builder, which includes over 125 VCL controls and over 100 different tools for creating...

weixin_42137539的博客 855

算法:全排列问题——邻位互换法

邻位互换法,只要你在学全排列就不可不学的一个及其有趣的算法

SkeletonKing233的博客 1753

WorkBuddy Skill赋能HYSYS与Comsol:AI工作台下的仿真自动化实践

AI Agent正将工业软件自动化带向新高度。在工程仿真领域,流程模拟与多物理场分析常因重复性操作而效率低下,Aspen HYSYS的批量工况调整、Comsol的参数扫描与网格重构均属此类。WorkBuddy这类AI工作台通过Skill将软件二次开发能力封装为可插拔技能包,利用HYSYS的COM/OLE接口和Comsol的Java API,实现自然语言指令到自动化执行的转变。其技术价值在于降低自动化门槛、沉淀工程经验,并支持日志审计与异常重试。在实际应用中,无论化工流程的灵敏度分析,还是激光焊接、移动网格等

weixin_29055699的博客 307

数字的排序

C语言中的数字排序法有很多:1.冒泡排序法2.选择排序法3.快速排序法4.比较排序法......

why 1528

关于LIO-SAM编译时出现错误 /usr/bin/ld: 找不到 -lBoost::timer

关于LIO-SAM编译时出现错误: 下载LIO-SAM代码 进入LIO-SAM文件夹,通过以下代码编译: mkdir build cd build cmake .. make 报错: /usr/bin/ld: 找不到 -lBoost::timer collect2: error: ld returned 1 exit status CMakeFiles/lio_sam_imuPreintegration.dir/build.make:759: recipe for target 'devel/lib/li

wwtx的博客 3648

内容审核与文本安全技术综述:检测、过滤与对齐

检测质量高度依赖数据。毒性/仇恨/冒犯语言检测领域形成了一批经典数据集,但它们的标签体系差异很大:例如 OLID(OffensEval)强调“是否冒犯—冒犯类型—目标对象”三层结构;HateXplain 在“hate/offensive/normal”三分类之外,还提供目标群体与人类标注的 rationale span;RealToxicityPrompts 则不做离散标签,而是用毒性分数来研究“模型续写时的毒性退化”。ACL 汇编。

nmdbbzcl的博客 1372

数字数组将相邻的数字排在一起

1.数字相邻,重新生成一个新的数组,相邻的数字差值大于2的情况下取最小和最大的数字 注意:请先将数组按升序排序 var array = [1,2,5,8,9,10,14,15,17,19,20,21,22,23,25]; function sort(arr){ return repeat(function(num,newArr){ if(newArr[0].length == 1 &a...

weixin_43248617的博客 2525

OCR文字识别基础

ocr文字识别

m0_47405013的博客 1755

Java将一个数组中相邻两个元素互换位置

JAVA,数组相邻元素互换位置的代码实现: 通过中间元素赋值(a=1,b=2----> temp=a, a=b,b=temp; 实现a=2,b=1)

huhahahah的博客 1947

YOLOv8优化:独家创新(SC_C_Detect)检测头结构创新,实现涨点 | 检测头新颖创新系列

独家创新(SC_C_Detect)检测头结构创新,适合科研创新度十足,强烈推荐

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

PCB高速信号换层孔新技术:VECS(Vertical Conductive Structure Technology)解析

摘要:垂直导电结构(VeCS)是一种新型PCB互连技术,由平板信号走线和椭圆形接地层构成半同轴结构,相比传统过孔具有显著优势。通过全波仿真和实测验证,VeCS展现出更好的阻抗连续性、更低的插入损耗和回波损耗,以及优异的串扰抑制能力。其椭圆形接地结构能有效隔离电磁干扰,在相同布线密度下远端串扰比传统过孔降低约50%。VeCS制备工艺与传统PCB工艺兼容,无需新增设备,且能实现更高密度互连。测试结果显示,2.5英寸带状线通道中VeCS结构的信号完整性表现明显优于传统过孔,为高速数字系统提供了更可靠的垂直互连解决

小孟同学的博客 202

数组冒泡排序

开发工具与关键技术: MyEclipse Java 撰写时间:2020、8、8 数组是编程语言的重要结构之一,数组常用于储存固定大小的同类元素。数组同时也有许多用法,比如遍历,排序等许多为开发提供便利的方法。 其中数组的排序也有许多排序方法,这里我说的是数组的冒泡排序,冒泡排序也是我们比较常用的排序方法之一。 首先我说一下我这个方法的原理大概是:他是通过数组角标相比较的方式进行排序,比如一个数组有5个数,相对应的就有5个角标,比较过程就是:第一轮比较拿0角标跟1角标比较数据大的一方往后置换顺序,置换完

weixin_44551879的博客 1212

web渗透--9--识别web应用框架

1、概述 Web框架指纹识别是信息收集过程中的一个重要任务。获悉目标框架的类型必然为测试人员带来极大的优势,如果该框架在之前就被测试过。测试人员不仅可以获得未修补版本中存在的漏洞,还可以了解此框架中特有的错误配置以及已知的文件结构。从而使指纹识别过程显得格外重要。

武天旭的博客 5101

java数组排序,查找与置换

一、排序 排序:将杂乱无章的数据元素,通过一定的方法按关键字顺序排列的过程叫做排序。 排序也是计算机内经常进行的一种操作,其目的是将一组“无序”的记录序列调整为“有序”的记录序列。

专栏 2953

Statistics and Machine Learning Toolbox

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

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

CSL 的魔法(非相邻交换成升序)

链接:https://ac.nowcoder.com/acm/contest/551/E 来源:牛客网 CSL 的魔法 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言1048576K 64bit IO Format: %lld 题目描述 有两个长度为 n 的序列,a0,a1,…,an−1a0,a1,…,an−1和 b0,b1,…,bn−1b0,...

ccsu_deer 933

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【置换环】CF1768D Lucky Permutation

置换环详解

蒟蒻的OI路 604

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

数组排序的两种方式

一个是先明确左边,一个是先明确右边; 一个是选择排序,一个是相邻排序; 选择排序: 1.先明确左边的值,(如果是从大到小排序,那就先明确最大值)    //通过x来限制左边的值的位置    for(x=0;x            //通过y值来限制右边的值的位置            for(y=x+1;y                   //拿左边的值

OMG_2012的专栏 741

【置换环问题】逐层排序二叉树所需的最少操作数目

第一次遇到置换环问题,记录一下~

m0_52309496的博客 455

Java---数组练习-获取最值,选择、冒泡排序

1、获取数组中的最大值、最小值 /* 给定一个数组{5,1,6,4,2,8,9} 1、获取数组中的最大值,及最小值 */ public class ArrayTest { public static void main(String[] args) { //int类型数组 int arr[] = {5,1,6,4,2,8,9}; int max = getMax(arr); ...

小码农1024的博客 497

数组冒泡排序和元素互换

一维数组冒泡排序: 基本思想: 对比相邻的值,如果满足条件就交换元素值,把较小的元素值移动到数组的前面,把大的元素值移动到后面,这样较小的元素就像气泡一样从底部上升到顶部。 package Item; public class Sort{ public static void main(String[] args) { int a[]= {12,54,36,2,8}; int i,j,temp; for(i=1;i<a.length;i++) { for(j=0;j<a.le

woreaijava的博客 392

poj 3270 Cow Sorting(初涉置换群)

http://poj.org/problem?id=3270 大致题意:给出n个整数,要将它们转化成递增序列,每交换其中两个数的代价是这两个数之和。问排序成功后的最小代价。 该题考察的是置换群知识。在黑书p247上有详细的讲解。总结下置换群,方便复习。 群:给定一个集合G={a,b,c...}和集合G上的二元运算 ·,如果满足封闭性,结合律,存在单位元和逆元,则成集合

1313

Python算法---冒泡排序算法以及插入排序算法

前言:算法是很重要的,但是太过于深奥又难以让人理解,那么本博文咱们就介绍两种简单通俗易懂的排序算法,即:冒泡排序和插入排序— 算法:一.冒泡排序:二.插入排序: 一.冒泡排序: 冒泡排序法:(相信大家都见过鱼泡泡吧,这就是那个道理,大的泡泡就会浮出来) 每次比较两个数,如果他们顺序错误(大于或者小于),那么就把他们置换 冒泡排序就是把小的元素往前调或者把大的元素往后调。比较是相邻的两个元素比较,交换也发生在这两个元素之间。 所以,如果两个元素相等,是不会再交换的; 如果两个相等的元素没有相邻,那么即使通过前

凭时间赢来的东西,时间肯定会为之作证 8万+

【数组】剑指 Offer 03. 数组中重复的数字 & 217. 存在重复元素 & 219. 存在重复元素 II & 220. 存在重复元素 III

剑指 Offer 03. 数组中重复的数字 题目 找出数组中重复的数字。 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 示例 1: 输入: [2, 3, 1, 0, 2, 5, 3] 输出:2 或 3 解法 Set辅助法 由于只需要找出数组中任意一个重复的数字,因此遍历数组,遇到重复的数字即返回。为了判断一个数字是否重复遇到,使用集合存储已经遇到的数字,如果遇到的一个

兰陵鸡王的博客 243

Scala数组相关的操作

数组这一章需要注意的要点是: 若长度固定则使用Array,若长度可能有变化则使用ArrayBuffer 提供初始值时不要使用new 用()来访问元素 用for(elem <- arr)来遍历元素 用for(elem <- arr if …) … yield … 来将原数组转型为新数组 Scala数组和Java数组可以互操作;用ArrayBuffer,使用scala.collection.JavaC

积微致著 3198

D. Lucky Permutation(置换环)

Codeforces Round #842 (Div. 2) D. Lucky Permutation

yuanxinya_的博客 2216
上一篇: 数组排序
下一篇: switch表达式
飞鸿踏雪泥-
博客等级 码龄12年 4粉丝 48原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值