Python实现基数排序算法

Python 专栏收录该内容
¥59.90 ¥99.00
285篇文章

基数排序是一种非比较性的排序算法,它将待排序的元素分成不同的位数,根据每个位上的数字进行排序。这篇文章将为你展示如何使用Python实现基数排序算法,并提供相应的源代码。

基数排序算法的主要思想是从最低有效位(LSB)到最高有效位(MSB)逐个排序。在每个位上进行排序时,可以使用稳定的排序算法,比如计数排序或桶排序。基数排序的时间复杂度是O(d * (n + k)),其中d是最大数字的位数,n是元素个数,k是每个位上的可能取值范围。

下面是Python实现基数排序算法的源代码:

def counting_sort(arr, exp):
    n = len(arr)
    output = 
python实现·十大排序算法之基数排序(Radix Sort) 简介 基数排序(Radix Sort)是一种非比较型整数排序算法,是桶排序的扩展。基本思想是:将所有待比较数值统一为同样的数位长度,数位较短的数前面补零。按照低位先排序,分别放入10个队列中,然后采用先进先出的原则进行收集;再按照高位排序,然后再收集;依次类推,直到最高位,最终得到排好序的数列。对于数值偏小的一组序列,其速度是非常快的,时间复杂度达到了线性,而且思想也非常的巧妙。 算法实现步骤 取得数组中的最大数,并取得位数; 对数位较短的数前面补零; 分配,先从个位开始,根据位值(0-9)分别放到0 阅读详情

相关推荐

MSD基数排序算法的Python实现

在这篇文章中,我们将详细介绍MSD基数排序算法的实现,并提供相应的Python代码。从最高位开始,按照当前位的数值将待排序数组中的数字分配到对应的桶中。例如,如果当前位的数值是2,则将所有以2开头的数字放入编号为2的桶中。函数中,我们创建了10个桶,并根据当前位的数值将数字分配到对应的桶中。最后,我们按照从0到9的顺序依次取出桶中的数字,并将它们组成新的有序数组。在初始状态下,这些桶是空的。中,我们首先找到待排序数组中的最大数字,并确定其位数。将所有桶中的数字按照从0到9的顺序依次取出,组成新的数组。

BugHunterX的博客 212

Python实现基数排序算法详解

对于每个数字,我们先取出当前位数上的数字digit,并将其放入对应的桶中。总结一下,基数排序算法是一种高效的排序算法,适用于待排序的元素均为非负整数的情况。基数排序算法是一种非比较性质的排序算法,它能够处理整数并将其按照位数分别比较后排序。这个算法的时间复杂度为O(nk),其中n是待排序元素的数量,k是元素的最大位数。在函数内部,我们首先获取了数组中的最大值,并计算出最大值的位数max_digit。我们可以通过调用radix_sort函数来进行基数排序,该函数的参数是一个整数数组,返回一个已排序的新数组。

带你成为别人眼中的大佬! 239

基于Python的基数排序算法实现

基数排序的时间复杂度为O(d * (n + k)),其中d是元素的位数,n是元素的个数,k是每个位的取值范围。基数排序(Radix Sort)是一种非比较性排序算法,它根据元素的每个位上的值进行排序。它适用于元素的位数相同的情况下,可以实现稳定的排序。基数排序的基本思想是将待排序的元素按照低位到高位的顺序进行排序。通过基数排序算法,我们成功将待排序的数组按照从小到大的顺序进行了排序。b. 按照桶的顺序,将元素依次取出,形成新的待排序序列。a. 将待排序元素按照当前位的值放入相应的桶中。

CodeHeroicX的博客 90

Python:实现msd 基数排序算法(附完整源码)

Python:实现msd 基数排序算法(附完整源码)

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

CISSP/CCSP证书到手后别松懈!保姆级维持攻略:CPE积分怎么攒?年费怎么交?

本文为CISSP/CCSP持证者提供详细的维持认证攻略,重点介绍如何高效积累CPE积分和避免年费缴纳中的常见问题。通过免费与付费资源结合的策略,帮助持证者轻松完成年度认证要求,同时利用认证创造职业价值。

weixin_30859423的博客 415

Gartner DSG数据安全治理 架构及解读

目录背景:架构图: 解释:英文解释:(自上而下)平衡业务与风险、威胁、合规之间的关系(5个维度之间的平衡)优先处理数据集制定安全策略实施安全工具策略配置同步从上到下,从需求调研开始实施数据安全治理。千万不要跨过数据摸底、治理优先级分析、制定治理整体策略,而直接从技术工具开始对数据安全进行治理。 先明确选择需要治理的对象,优先选择重要数据进行数据安全治理工作。可以通过数据梳理、分类分级等手段,完成数据资产盘点和筛选,在纷杂的数据中理清头绪。

锦慈的技术博客 7216

R语言笔记十:独立性检验函数

1.独立性检验函数        独立性检验是根据频数信息判断两类因子彼此相关或相互独立的假设检验。所谓独立性就是指变量之间是独立的,没有关系。 独立性检验算法:卡方检验、Fisher检验、Cochran-Mantel-Haenszel检验 2.假设检验 假设检验(Hypothesis Testing)是数理统计学中根据一定假设条件由样本推断总体的一种方法。 原假设——没有发生 备择假设——发生了 具体做法:根据问题的需要对所研究的总体作

weixin_44384631的博客 4111

用户行为数据分析 User Behavior Data from Taobao for Recommendation

一、提出问题 二、理解数据 数据来源 阿里云天池数据集:(https://tianchi.aliyun.com/dataset/dataDetail?dataId=649) 本文选取的数据集为UserBehavior.csv,共有5242880条数据。数据的具体类别如下表所示: 用户行为种类有四种,如下表所示 对于数据维度的说明如下: 在本文选取的2017年11月25日到2017年12月3日的时间范围内,不重复的用户数量为10202,不重复的商品ID数为412369,不重复的商品类目ID数为5859,

cccxxxqq的博客 5205

Agent应用实践之三十六 - AgentScope高阶用法:AG-UI

AI 大模型飞速进化,不懂 AI、不会用 Agent 的程序员,正在慢慢被行业淘汰。现在 AI 新技术迭代快到离谱:RAG 还没学明白,MCP 接踵而来;工具刚上手,Skills、各类新 Agent 概念又轮番刷屏;Manus还没看懂,就出来OpenClaw,OpenClaw热度还没退,又出来Hermes。越追越乱、越学越焦虑,永远在被动跟风,陷入学不完、跟不上、用不上的内耗。其实焦虑的根本原因,不是新技术太多,而是不懂大模型 Agent 应用底层原理,只会零散跟风,没有通用方法论。为此我专门推出Agent

代码让AI扣 139

67、全球导航卫星系统授时弹性分析与建议

本文分析了全球导航卫星系统(GNSS)在授时过程中面临的问题,包括系统自身异常和外部干扰欺骗,并探讨了提升授时弹性的关键技术与解决方案。文章介绍了美国、欧洲及中国在弹性授时系统建设方面的进展,重点剖析了高精度原子钟、时间同步、干扰检测与识别等核心技术,提出了GNSS与其他授时系统互补的集成化发展路径。最后展望了未来授时系统向更高精度、更强弹性、更广应用的发展趋势,为构建安全可靠的国家授时体系提供参考。

corn8的博客 185

11、系统稳定性分析与李雅普诺夫函数的应用

本文深入探讨了李雅普诺夫函数在系统稳定性分析中的核心作用,重点介绍了时变与时不变系统的稳定性判据差异,以及巴尔巴拉特引理在处理非自治系统中的应用。文章还阐述了正线性系统与严格正实传递函数的性质及其在非线性控制系统中的重要性,并展示了李雅普诺夫函数在评估主导时间常数和设计快速干扰抑制控制策略方面的实际应用。通过具体示例和流程图,系统地总结了李雅普诺夫方法在稳定性分析、性能评估与控制器设计中的多维度价值,同时指出了当前挑战与未来发展方向。

Jerry的专栏 93

如何为 Claude Code 配置代理与网络环境

那么大概率不是软件问题,而是。因为 Claude Code 是运行在终端里的,不是浏览器。

sg_knight的专栏 3479

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

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

上一篇: Python翻转字符串】- 轻松实现字符串反转
下一篇: Python多进程编程详解
普通网友
博客等级 码龄3年 105粉丝 492原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值