C#: 写一个循环排序算法——Cycle Sort(完整源码附上)

C# 专栏收录该内容
¥59.90 ¥99.00
136篇文章

C#: 写一个循环排序算法——Cycle Sort(完整源码附上)

循环排序算法(Cycle Sort) 是一种比较高效的排序算法,适用于处理各种数据类型,包括整数、浮点数、字符串等。与其它排序算法相比,循环排序算法的时间复杂度低,是一种相对快速的排序方法。

循环排序算法的基本思想是:通过多次循环遍历数组,并将数组中的每个元素都与其他元素进行比较,然后将其正确地放置在数组中的指定位置上。在这个过程中,我们不断地调整元素的位置,以便在最终数组中获得有序的结果。

下面是 C# 语言实现循环排序算法的完整源代码:

using System;

public class CycleSort
{
   
   
    public static v
C#实现圈排序Cycle Sort算法 Sort方法实现了圈排序算法的主要逻辑,其输入参数为一个整型数组,并且不需要返回值。在Sort方法内部,我们首先获取数组的长度,然后进行逐个元素的遍历,一次循环就代表着一个子圈的遍历。对于每一个子圈,我们会首先获取当前位置的元素并且记录下它的位置,然后向后遍历找到当前元素应该放置的位置。否则,我们将找到的位置上的元素与当前元素交换,并且继续进行位置追踪,直到找到原来的位置。该算法思想是通过不断将当前位置的元素放到应该属于它的位置上来进行排序,避免了无谓的比较和交换操作,从而使得时间复杂度大大减少。 阅读详情

相关推荐

Cycle Sort循环排序算法的C#实现

尽管循环排序算法的时间复杂度为O(n^2),但由于最小化了元素的写操作,对于某些特定情况下的数据集,循环排序算法可能会比其他排序算法更高效。然后,我们使用两层嵌套的循环遍历数组,找到每个元素在有序序列中的正确位置,并将其移动到该位置上。在移动元素的过程中,我们还需要处理重复元素的情况,确保每个元素都被正确排序。循环排序算法(Cycle Sort)是一种不稳定的排序算法,它的特点是最小化元素的写操作。该算法的基本思想是通过不断地将每个元素放到它在有序序列中的正确位置上,直到所有元素都被排序完成。

AhTf__的博客 167

C#:实现圈排序Cycle Sort算法 (附完整源码)

C#:实现圈排序Cycle Sort算法 (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 332

C#:实现 Cycle Sort循环排序算法(附完整源码)

C#:实现 Cycle Sort循环排序算法(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 313

Cycle Sort(圈排序)是一种原地、不稳定的排序算法,它主要用于对数组进行排序

它的特点是最小化元素的移动次数,适用于那些对元素的写入操作比较昂贵的情况。在内层循环中,我们通过比较当前元素curr与item的大小来确定item应该在的位置pos。如果pos发生了变化,我们将item与pos位置的元素交换,并将pos位置的元素记为curr。然后,我们使用一个循环,不断地将curr放到它应该在的位置上,直到它回到起始位置。虽然它的时间复杂度较高,但由于最小化了元素的移动次数,适用于某些特定的应用场景。最后,我们重复外层循环,对下一个未排序的元素执行相同的步骤,直到整个数组排序完成。

DevScript的博客 390

Cycle Sort算法:一种高效的排序算法

Cycle Sort算法是一种高效的排序算法,尤其适用于小规模数据集的排序。相比于其他排序算法,Cycle Sort算法的优点在于最小化了交换操作的次数,从而减少了计算开销。因此,对于小规模数据集的排序需求,Cycle Sort算法是一个不错的选择。Cycle Sort算法是一种基于交换的排序算法,它具有较低的时间复杂度和空间复杂度,适用于小规模数据集的排序。Cycle Sort算法的核心思想是通过一系列的循环来将每个元素放到它应该在的位置上。Cycle Sort算法:一种高效的排序算法。

DevGOOD的博客 230

圆形排序(Cycle Sort)算法的C#实现

圆形排序(Cycle Sort)算法是一种比较简单且高效的排序算法,它通过最小化交换的次数来对数组进行排序。希望本文能够帮助您理解圆形排序算法的工作原理及其在实际应用中的用途。已经位于正确的位置上,我们可以直接跳过当前循环。然后,我们使用内层循环遍历数组中剩余的元素,找到比。,并将其初始化为一组无序的元素。的元素进行交换,并重复执行交换操作,直到将。2),因此它在大多数情况下是一种不错的选择。在外层循环中,我们选择当前循环的起始位置。小的元素的数量,并将其存储在变量。,并将其对应的元素作为待排序项。

DarcyCode的博客 325

波束形成算法:LCMV实现(MATLAB)

阵列信号处理,波束成形,

weixin_43601588的博客 1万+

零外围电路!用CH340X实现ESP32一键下载的完整教程

本文详细介绍了如何利用CH340X系列USB转串口芯片实现ESP32的一键自动下载功能,无需任何外围逻辑控制电路。通过解析CH340X的增强型DTR/RTS功能,提供核心电路设计、电压匹配方案,并指导在Arduino IDE和PlatformIO中进行软件配置,帮助开发者构建简洁、可靠且低成本的ESP32下载方案。

gpu4optimizer的博客 795

点云配准实战:用DGCNN+Transformer+SVD搞定3D模型对齐(附PyTorch代码)

本文详细介绍了基于深度学习的点云配准实战方法,重点解析了Deep Closest Point (DCP)模型。该模型融合了DGCNN、Transformer和可微分SVD三大核心模块,能够端到端地预测点云间的刚性变换,有效克服传统ICP算法对初始位置敏感、易陷入局部最优的缺点。文章提供了完整的PyTorch代码实现、ModelNet40数据集处理、训练策略及显存优化技巧,旨在帮助开发者在机器人、自动驾驶等领域实现精准的3D模型对齐。

weixin_29061041的博客 162

app编写需要哪种语言

原生App开发:优选Swift(iOS)和Kotlin(Android)。跨平台App开发:优选Flutter(Dart)和React Native(JavaScript)。游戏App开发:优选Unity(C#)和Unreal Engine(C++)。低代码工具:适合简单、快速的App制作。

fanying_98的博客 2075

静止无功补偿SVG的基本控制原理

上图是自励单变量控制框图,基本控制方式为,采集计算得到负载无功电流和SVG输出的无功电流的差值,经过PI调节作为功率角δ和固定的调制深度λ和调制波,经过PWM调制形成开关信号驱动SVG的功率电路。该控制方式使保持调制深度λ(即SVG电压的幅度)不变,通过调整δ来调整直流电压和无功,跟踪输出无功功率,直流电压就会大幅度随动,无功相应速度较慢。SVG通常有他励方式和自励方式,他励方式需要提供外部的直流电源,自励方式通过电网输出有功功率至SVG建立SVG的直流电压。δ为Us和Uc之间的相角差。

weixin_42668920的博客 2006

【C++】auto关键字(C++11,超详细解析,小白必看系列)

本文详细解析了C++中的“auto”关键字,介绍了其定义、基本用法、限制和高级用法,特别是与范围for循环和Lambda表达式结合的应用。

m0_73494049的博客 6万+

告别WPS与Office兼容性噩梦:用Aspose.Words生成Word/PDF时统一页码的终极方案

本文深入探讨了使用Aspose.Words解决WPS与Office文档页码兼容性问题的终极方案。通过分析分页符处理机制的差异,提供Java代码实现智能检测和移除问题分页符的方法,确保Word/PDF文档在不同平台显示一致的页码,特别适用于合同、标书等对格式要求严格的场景。

weixin_27966471的博客 144

Qt多线程实战:如何优雅地处理Modbus设备通信中的线程安全问题

本文深入探讨了在Qt框架下实现Modbus设备通信时的多线程安全问题,提供了优雅的解决方案。通过生产者-消费者模型、任务队列与锁机制等设计模式,有效解决了界面卡顿、数据包错乱等问题。文章还详细介绍了对象生命周期管理、信号槽连接类型选择等关键实现细节,帮助开发者构建稳定可靠的Modbus通信模块。

weixin_30897233的博客 489

老版QQ原生表情gif动态

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

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

深度学习工具包 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#实现寻找第k个最小元素算法(附完整源码)
下一篇: 使用C#实现鸡尾酒排序算法 - 代码实现详解
普通网友
博客等级 码龄3年 162粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值