2932 Problem D 二分递归快排(Qsort) [2*]

Coder Pat之路 问题 D: 二分递归快排(Qsort) [2*] 问题 D: 二分递归快排(Qsort) [2*]时间限制: 1 Sec  内存限制: 128 MB题目描述二分递归快排(Qsort) 用二分递归的方法实现快排 输入:第一行一个数据n,表示有n个数要排序。接下来n行每行一个<=10^7的整数。 输出:n行,由小到大排序后的数据 数据规模:n<=10^5 思考:两个递归都会被执行吗?有几种可能?User: 吴锦诚 Date: 2018/6... 阅读详情

问题 D: 二分递归快排(Qsort) [2*]

时间限制: 1 Sec  内存限制: 128 MB
提交: 167  解决: 126

题目描述

二分递归快排(Qsort) 
用二分递归的方法实现快排 

输入

第一行一个数据n,表示有n个数要排序。接下来n行每行一个<=10^7的整数。 

输出

n行,由小到大排序后的数据 

数据规模

n<=10^5 

思考

两个递归都会被执行吗?有几种可能?

经验总结

基础的快速排序题~~

AC代码

#include <cstdio>
int Partition(int a[],int left,int right)
{
	int temp=a[left];
	while(left<right)
	{
		while(left<right&&a[right]>temp)	right--;
		a[left]=a[right];
		while(left<right&&a[left]<=temp)	left++;
		a[right]=a[left];
	}
	a[left]=temp;
	return left;
}
void quicksort(int a[],int left,int right)
{
	if(left<right)
	{
		int pos=Partition(a,left,right);
		quicksort(a,left,pos-1);
		quicksort(a,pos+1,right);
	}
}
int main()
{
	int n,no[100010],m;
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;i++)
			scanf("%d",&no[i]);
		quicksort(no,0,n-1);
		for(int i=0;i<n;i++)
			printf("%d\n",no[i]);
	}
	return 0;
}

 

二分查找 归并排序 快排 详解C++ 这三个排序算法一直是面试的重点,大多数都是C语言写的,今天整理了一下C++的写法,思想都差不多。这几个排序经常忘记,今天抽空记在这,以便自己以后查阅,不对的地方,也欢迎大家评论,不吝指正,谢谢! 二分查找(时间复杂度是O(logn)) 二分查找的基本思想是将n个元素分成大致相等的两部分,取a[n/2]与x做比较,如果x=a[n/2],则找到x,算法中止;如果xa[n/2],则只要在数组a的右半 阅读详情

相关推荐

快排与二分检索递归与非递归

快排与二分检索的递归与非递归算法分析与设计

问题 D: 二分递归快排(Qsort) [2*]

题目链接:http://codeup.cn/problem.php?cid=100000586&pid=3 题目描述 二分递归快排(Qsort) 用二分递归的方法实现快排 输入 第一行一个数据n,表示有n个数要排序。接下来n行每行一个<=10^7的整数。 输出 n行,由小到大排序后的数据 代码 #include<stdio.h> const int maxn = 501...

Rhao的博客 452

电源完整性仿真流程梳理

可根据仿真流程导图,一步一步的去设置完成仿真。仿真的关键,在于对电路回路的理解以及所放置的电压源与电流的合理性,对于去耦电容的选择,则需要根据谐振频率计算,从而选取有效滤波的容值。

csy17729158342的博客 3450

问题 C: 快速排序 qsort [2*]

问题 C: 快速排序 qsort [2*] 时间限制: 1 Sec  内存限制: 128 MB 提交: 210  解决: 116 [提交][状态][讨论版][命题人:外部导入] 题目描述 输入n个整数,用快速排序的方法进行排序 Input 第一行数字n 代表接下来有n个整数 接下来n行,每行一个整数 Output Output 升序输出排序结果 每行一个数据 Sample In...

Hacker_Wind的博客 496

[Linux项目实践] LCD Panel的硬件分析及调试

LCD硬件分析 项目使用3.5” TFT-LCD并且支持触摸控制。LCD的硬件可从几个方面学习:1)查看LCD的硬件连线。2)查看数据手册了解寄存器的控制。3)查看LCD DATASHEET了解硬件操作 1)LCD硬件连线 在图中有33个引脚,22根数据引脚和9根控制引脚。 LCD的工作原理:像素点是从左至右,从上至下逐个显示,LCD控制寄存器会从从内存中的framebuffer通

Terry_Yuan的技术专栏 3029

别再硬编码了!WinCC子画面动态加载的5种C脚本写法对比

本文深入解析WinCC子画面动态加载的5种C脚本实现方案,包括基础加载、路径动态解析、多语言切换、窗口自适应布局和混合架构交互。通过代码示例和场景分析,帮助开发者避免硬编码问题,提升工业自动化系统的交互效率和可维护性。特别推荐路径动态解析方案,有效解决项目路径管理的痛点。

weixin_29008691的博客 482

快速排序(二分思想,递归)

p话少说,自己看代码 #include <iostream> #include <cstdio> #include "common.h" using namespace std; int a[101]; int n; void QSort(int left, int right) { int i; int j; int t; int key; if (lef...

d1781516638的博客 600

Python爬虫实战:5分钟搞定东方财富网股票数据抓取(附完整代码)

本文详细介绍了如何使用Python爬虫快速抓取东方财富网的股票数据,包括环境配置、API分析、核心代码实现及优化技巧。通过简单的requests和pandas库,5分钟内即可获取高质量股票数据,适合量化交易和数据分析初学者。

weixin_29040367的博客 1198

2932 Problem D 二分递归快排(Qsort) [2*]

问题 D: 二分递归快排(Qsort) [2*] 时间限制: 1 Sec  内存限制: 128 MB 提交: 82  解决: 65 [提交][状态][讨论版][命题人:外部导入] 题目描述 二分递归快排(Qsort)  用二分递归的方法实现快排  输入:第一行一个数据n,表示有n个数要排序。接下来n行每行一个&lt;=10^7的整数。  输出:n行,由小到大排序后的数据  数据规模:n&lt...

子衿 412

手把手教你学Simulink--控制执行场景实例:基于Simulink的智能车辆自动紧急制动(AEB)仿真

本文完成了基于 Simulink 的智能车辆自动紧急制动(AEB),实现了:✅ 构建前车急刹与静止障碍物典型场景✅ 设计基于 TTC 的三级 AEB 决策逻辑✅ 验证在高风险场景下有效避免碰撞✅ 掌握从风险评估到制动执行的完整安全链✅ 为ADAS 主动安全功能开发奠定基础。

MHD0815的博客 243

ENVI:如何进行对自带RPC的图像进行RPC正射校正呢?

但是我们知道,人生哪里有那么多1就是1,2就是2的情况,更多的实际上是非线性的关系,所以说RPC模型(基于线性多项式模型的进一步改进)会比线性多项式模型更为妥善的解决几何校正的问题。一幅图像上的每一个像素都有它的像点坐标(x,y)(即以图像的左上角点为原点,该像素在整幅图像的哪一个行(y),哪一列(x)),以及该像素所在实际位置的DEM高程数据。那么也就是说,对于一幅图像上的每一个像素,都需要知道该像素所在的行数(y),所在的列数(x),所处位置的DEM高程数据(h) ==> 坐标(x,y,h)。...

炒茄子的博客 5684

『算法笔记』- 01 - C++ 实现:快速排序、归并排序 & 二分

📘At first:一个初学算法的萌新,如果文中有误,还请指正🤓🎗️专栏介绍:本专栏目前基于AcWing算法基础课进行笔记的记录,包括及课上大佬讲的一些算法的模板还有自己的一些心得和理解🕶️个人博客地址:https://blog.csdn.net/m0_73352841?一、快速排序。快速排序的思想是分治 1.1 步骤 1、先确定分界点:可以是左边界,中间值、右边界或者任意位置都行 2、调整区间:小于等于x的在左半边,大于等于x的在右半边(不用纠结那个中间的位置),『难点』💣💣💣 3、递归处理

Where there is a will there is a way 581

从零到一构建一个智能爬虫 Agent 完整教程

本文将打造一个具备自主决策、动态解析、反反爬、持久化能力的智能爬虫 Agent

数据知道的博客 4803

算法基础课01:快速排序、归并排序、二分

1.快速排序 快速排序的基本思想是分治: ①确定分界点X:常用的方法是取左端点q[l]、右端点q[r],中点q[(l+r)/2]或者随机。不过一般取中点。 ②调整区间:将所有≤X的数都放在X的左半边,所有≥X的数都放在右半边(调整完后左边的数都≤X,右边的数都≥X) ③递归处理左右两段。 其核心就在于第二步调整区间。 对于第二步可看看以下两种做法: 方法一:暴力做法: ①建立两个空白数组a[],b[]; ②遍历数组q[l~r],对其中≤X的元素q[i],放入数组a[];对于>X的元素

weixin_43843299的博客 2312

NSIS教程(8): 基于第三方界面库的安装包界面

前面的几篇文章介绍了NSIS的传统界面的安装包和现代界面的安装包的制作方法,也提到了NSIS支持自定义页面(即使用page custom)的特性,自定义页面需要用户自己创建对话框、控件、添加控件响应等等,虽然NSIS提供了nsDialogs.nsh来支持这些功能,但使用起来还是不太方便(需要专门了解这个插件诸多用法),而且不够灵活,所以本文介绍一种终极的自定义界面的安装包解决方案,即完全使用第三方

while(1) { smile(); } 9万+

Statistics and Machine Learning Toolbox

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

day01.快排+归并+二分

①快排 与 归并 的实现思想有相似之处,三步走。②整数二分与平常理解的二分查找(分三类)实现有差别,注意理解。整数二分的两种模板按需使用。③浮点数二分通过确定误差实现精度要求。

qq_62389417的博客 772

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

递归二分排序(快速排序)

递归二分法排序

weixin_43353869的博客 268

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

Codeup——586 | 问题 D: 二分递归快排(Qsort) [2*]

题目描述 二分递归快排(Qsort) 用二分递归的方法实现快排 输入:第一行一个数据n,表示有n个数要排序。接下来n行每行一个<=10^7的整数。 输出:n行,由小到大排序后的数据 数据规模:n<=10^5 思考:两个递归都会被执行吗?有几种可能? #include <iostream> #include <cstdio> using namespace std; int Pertition(int a[],int left,int right){ int temp=

qq_44888152的博客 1016

C++快速排序与二分查找的实现

快速排序用递归的方式实现,二分查找用循环的方法实现。也可以用qsort实现快速排序。 一个小小的应用: 要求两个集合的并集时,可以先对一个集合进行排序,再用二分查找会比较快。当然,改进的话可以对两个集合都进行排序,当找到某一位置时,直接将后面一大段移到另一个数组后面就可以了。 #include<iostream> using namespace std; //快速排序 void Qsort(int a[], int left, int right) { if (left >= right

Alask_的博客 589

算法C++ 快速排序递归代码实现(第二章)

文章目录造轮子博客链接我对快速排序的理解代码实现测试数据1实现效果1测试数据2实现效果2 造轮子博客链接 算法第四版C++算法实现全集 我对快速排序的理解 我认为快速排序 真的是必须必须必须要能够在15分钟之内手撕出来的 算法 重要性不言而喻 无论是面试还是考试其他的地方而言 除了对于面试这些其他之外 确实可能是最实用也是目前最快的排序算法了 就像C++的sort还是 用的也是快排 但是确实 我认为快排需要注意的地方太多了 我今天下午的英语课一点东西没听 就在研究看书 看快排 今天晚上在图书馆里面 把书

Love 6's Private Blog 543

C++算法和数据结构之《二分查找、斐波那契、快排》

文章目录一:二分查找二:斐波那契 一:二分查找 二分查找要求所查找的顺序表必须是有序的。定义left为顺序表最左端元素位置,right为顺序表右端元素位置。定义mid = (left + right) / 2,即顺序表的中间位置,然后用所查找的值与mid所在位置处的值比较,由于列表有序,若所查找的值比mid小,则只需在表的前半部分查找,否则只需在表的后半部分查找(若第一次比较就发现两值相等则直接返回当前值所在的位置),以此类推,直至查找到所寻找的值或确定所查找的值不在该列表内为止(即查找失败)。 代码:

qq_46615150的博客 473

归并/堆排/快排/二分---C++实现

#include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <string> #include <list> using namespace std; int binSearch(vector<int>arr,int start,int end,int key) { int

宫浩然的博客 554

快速排序 C++递归实现

快速排序 C++递归实现,包含时间复杂度和空间复杂度分析

aaa44123的博客 2694
上一篇: 2843 Problem C 快速排序 qsort [2*]
下一篇: 5067 Problem A 求第k大数
漫浸天空的雨色
博客等级 码龄15年 1303粉丝 523原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值