数据结构--快速排序

数据结构(C语言版) 快速排序 快速排序: 代码实现: 说明:本博客的代码实现贴近数据结构(C语言版) 课本代码风格,使用抽象数据类型。 项目结构 以下文件代码与直接插人排序时所用相同,为避免大量代码重复此处不再附上。 详情可参考直接插人排序:https://blog.youkuaiyun.com/ZipayYu/article/details/98245056 status.h ststus.cpp SequenceListType.h... 阅读详情

快速排序是对起泡排序的一种改进。
它的基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分的记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。

如何判别快速排序的结束?
若待排序列中只有一个记录,显然已有序,否则进行一次划分后,再分别对分割所得的两个子序列进行快速排序(即递归处理)。

int Partition(int r[],int first,int end)
{
    int i=first,j=end,temp; //初始化
    while(i<j)
    {
        //j从后向前扫描,直到r[j]<r[i],将r[j]移动到r[i]的位置,使关键码小(同轴值相比)的记录移动到前面去;
        while(i<j && r[i]<=r[j]) --j;   //右侧扫描
        if(i<j)
        {
            //将较小记录交换到前面
            temp=r[i];
            r[i]=r[j];
            r[j]=temp;
            ++i;
        }
        //i从前向后扫描,直到r[i]>r[j],将r[i]移动到r[j]的位置,使关键码大(同轴值相比)的记录移动到后面去;
        while(i<j && r[i]<=r[j]) ++i;   //左侧扫描
        if(i<j)
        {
            //将较大记录交换到后面
            temp=r[i];
            r[i]=r[j];
            r[j]=temp;
            --j;
        }
        //重复上述过程,直到i=j
    }
    return i;
}

void QuickSort(int r[],int first,int end)
{
    if(first<end)
    {
        int pivotpos=Partition(r,first,end);    //一次划分
        QuickSort(r,first,pivotpos-1);  //对前一个子序列进行快速排序
        QuickSort(r,pivotpos+1,end);    //对后一个子序列进行快速排序
    }

}

(1)最好情况:每一次划分对一个记录定位后,该记录的左侧子表与右侧子表的长度相同,为O(nlog2 (n))。
(2)最坏情况:每次划分只得到一个比上一次划分少一个记录的子序列(另一个子序列为空),为 O(n^2 )。
(3)平均情况:为O(nlog2 (n))。
(4)稳定性:不稳定的

大话数据结构系列之快速排序算法 文章目录实现思路重点知识代码实现优化策略算法比较与各位共勉 实现思路 1、属于冒泡排序的升级版,都是通过不断的比较和移动交换来实现排序,它的实现,增大了记录的比较和移动的距离,将关键字较大的记录从前面直接移动到后面,关键字较小的记录从后面直接移动到前面,从而减少了总的比较次数和移动交换次数。 2、通过一趟排序将待记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这... 阅读详情

相关推荐

数据结构实验之排序八:快速排序

数据结构实验之排序八:快速排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定N(N≤10^5)个整数,要求用快速排序对数据进行升序排列,注意不得使用STL。 Input  连续输入多组数据,每组输入数据第一行给出正整数N(≤10^5),随后给出N个整数,数字间以空格分隔。 Output  输出排序...

Move on 463

交换排序:快速排序

快速排序 #include<stdio.h> //用第一个元素将待排序序列划分成左右两部分 int Partition(int A[],int low,int high){ int pivot=A[low];//第一个元素作为枢轴 while(low<high){//用low、high搜索枢轴的最终位置 while(low<high&&A[high]>=pivot) --high; A[low]=A[high];//比枢轴小的元素移到左端 whi

qq_43707782的博客 345

C++数据结构--快速排序

1.对向量数组v{800,150,300,650,550,500,400,350,450,900}进行快速排序,索引[first,last)=[0,10)   *将v的元素划分为两个子表s1和S2,子表S1的元素都=中心值     step1:取pivot=v[mid]=v[5]=500,作为中心值,并与v[first]交换,其中mid=(first+last)/2           整数

piniheaven的专栏 988

快速排序

快速排序 是一种不稳定的排序算法,时间复杂度 O(nlog2 N)。 基本思想: ​ 首先选一个轴值(pivot,即比较的基准),将待排序记录划分为独立的两部分,左侧记录的关键码均小于或等于轴值。右侧记录的关键码均大于或等于轴值,然后分别对这两部分重复上述过程,直到整个序列有序。 代码: int Partition(int r[],int first,int end)//一次划分算法 { ​ in...

questionhahaha的博客 278

安装Tailscale

如果返回 /usr/bin/tailscale 或类似路径,说明已安装但 sudo 可能找不到它。如果 which tailscale 没有输出,说明安装失败,重新执行安装步骤。Tailscale 的后台服务 tailscaled 必须运行。检查 tailscaled 服务是否运行。检查 tailscale 命令是否存在。

FairLikeSnow的博客 1476

将Apollo高精地图中的车道线叠加在多种导航地图上

本文介绍了将Apollo高精地图车道线叠加到普通导航地图的技术方案。高精地图具备厘米级车道精度和丰富的语义信息,叠加可实现可视化验证、调试分析和数据对比等功能。核心技术包括坐标系统转换(UTM/WGS84/BD09/GCJ02)和数据处理流程,通过Python脚本完成Apollo地图解析、坐标批量转换和多格式输出。文章详细提供了环境准备、工作目录创建和坐标转换脚本等操作步骤,实现了高精地图数据在OpenStreetMap、百度地图和高德地图上的可视化展示。

Hi20240217的博客 1333

【排序算法】快速排序

快速排序和归并排序一样,都是应用了分治法的排序算法 一. 算法思想 划分:选定一个记录作为轴值,以轴值为基准将整个序列划分为两个子序列r1r_1r1​…ri−1r_{i-1}ri−1​和ri+1r_{i+1}ri+1​…rnr_nrn​,轴值的位置i在划分的过程中确定,并且前一个子序列中的记录都小于或等于轴值,后一个子序列中的记录都大于或等于轴值 求解子问题:分别对划分后的每一个子序列递归处理 合...

小仓猪的博客 410

大整数因数分解工具——yafu

可以将大整数放在一个新建文件中——data.txt,最后面一定要换行,不然会出现eof;done processing batchfile,运行命令。2.点击设置——系统——系统信息——高级系统设置——环境变量——点击PATH(上下都可以)——新建 添加yafu-x64.exe 所在路径——点击确定。,如果整数数过长,会出现错误mismatched parens;1.找到yafu-x64.exe 所在的文件路径。如果出现以下图像,说明配置成功。3.打开cmd,输入。

l2ohvef的博客 6156

排序

1076 排序给出n和n个整数,希望你从小到大给他们排序。自己写冒泡复杂度n方肯定超时了。stl sort可以a,但是自己不太建议这么来。高级题解里1.快排 2.qsort 3.归并排序1.快排#include&lt;iostream&gt; using namespace std; int Partition(int r[], int first, int end) //划分 { i...

骑鱼的喵喵的博客 187

ubuntu22.04下对使用linuxdeployqt发布的qt程序进行deb打包操作

【代码】ubuntu22.04下对使用linuxdeployqt发布的qt程序进行deb打包操作。

十三阿哥的博客 1402

ESP32-C3入门教程 基础篇⑩——SNTP(Simple Network Time Protocol) 网络时间同步的简单实例

ESP32-C3入门教程 基础篇⑩——SNTP(Simple Network Time Protocol) 网络时间同步的简单实例

1589

数据结构笔记——第八章 排序技术

8.1   概述 8.1.1   排序的基本概念 记录:在排序问题中,通常将数据元素。 排序: 将一个记录的任意序列重新排列成一个按关键码有序的序列。 排序算法的稳定性:在原序列中,ki=kj,且ri在rj之前,在排序后的序列中,ri仍在rj之前,为排序算法稳定性;否则称为不稳定。 排序分类:插入、交换、选择、归并。 8.1.2  排序算法的性能 通常需要进行以下两种操作:1)比较,

mengmeihq888的专栏 498

RAP 语义键:让 UUID 世界变得可读、可导航、可校验

RAP 语义键的核心价值与实现要点 语义键(Semantic Key)是 ABAP RAP 模型中连接技术实现与业务体验的关键设计:通过为实体指定一组业务可读的标识字段(如订单号、物料编码等),在保持技术主键(如UUID)不变的同时,显著提升用户界面的可读性和导航体验。 主要优势: 提升UI可读性:Fiori元素会突出显示语义键字段,帮助用户快速识别关键业务数据 改善导航体验:OData URL会使用可读的语义键而非技术ID,便于分享和排查问题 降低沟通成本:业务人员能准确引用有意义的业务标识而非技术ID

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 155

快速理解数据结构之快速排序

前段时间一直在学习数据结构,在看快排时会有点小困难,不知有没有小伙伴跟我一样笨呢! 不过幸好查看完在各大资料后理解了! 接下来讲下快速理解的方法,希望对初学懵懂者有用! 以下例子 [4、2、3、1、6、7、10、8] 1.最先开始要找一个为基准,这个基准是最后才放进去的,是先存储到一个指针里面,一般是找第一个数为基准,即4 [4、2、3、1、6、7、10、8] 4 2.可以在源码中...

JEROCHAN的博客 318

基于VLFM的中文指令视觉语言导航系统设计与实现

文章摘要 本文介绍了一个基于VLFM(视觉语言边界地图)的中文指令视觉语言导航系统设计与实现。该系统采用模块化架构,包含三维场景渲染、URDF机器人仿真、VLFM导航引擎、中文指令解析和PyQt图形界面五大核心模块。系统支持自定义GLB场景模型和URDF机器人模型,通过transformers和langchain实现中文自然语言指令解析,利用VLFM算法进行语义导航决策。技术实现上详细介绍了GLB模型加载器开发、URDF机器人控制、VLFM导航算法集成以及PyQt界面设计。该系统在Ubuntu环境下运行,依

AI的内部世界的博客 208

适航规章、标准,书籍资料下载汇总(更新中)

以下是一些收集到的适航相关的资料,分享给大家。(如果没有你想要的资料,请留言,等找到会及时更新!

qq_41854911的博客 2553

数据结构中数组的快速排序算法

— 选择一个基准值(pivot),将数组分成两部分,左边都比基准小,右边都比基准大,然后递归处理左右两部分。

weixin_67377649的博客 719

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

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

数据结构试验-快速排序-堆排序

这是最后一个实验,我自己的信念是认真对待自己想要去做的事情,哪怕是你在帮,都不要放开它,坚持自己的选择,踏实的走下去,这条路需要慢慢积累! 这次直接上传代码: 文件1  t11.h #include"stdio.h" #include"string.h" #include"ctype.h" #include"malloc.h" #include"stdlib.h"  //atoi()

我的IT追求 1338

计算机控制技术及应用

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

数据结构之快速排序

11111111

太白子的博客 201

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

数据结构中的排序--快速排序

快速排序可以先参考:https://baike.baidu.com/item/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/369842?fr=aladdin #include "stdafx.h" #include <iostream> using namespace std; void print(int dat...

仗劍走天涯 506

数据结构排序之快速排序算法

在排序中快速排序是在冒泡排序的基础上进行算法优化而得出的,很大程度上见减小了冒泡排序的时间复杂度。可以说快速排序算法无愧于它的名字,高速高效。 接下来将介绍快速排序的思想以及相应算法 快速排序 快速排序的思想其实很简单,其实就是分治思想。大概的意思是在一组数中以一个数为基准,讲比它的数扔到它的两边,将小的扔到它的左边,将比它大的扔到它的右边(以升序为例),并持续这种操作,进而实现排序的目的。...

qq_42057046的博客 1万+

Day72.快速排序 -数据结构

????快速排序 学习资源来源: 浙大 数据结构

阿昌爱Java 334

数据结构·交换排序(冒泡排序、快速排序)

本文详细介绍了两种经典的交换排序算法:冒泡排序和快速排序。首先,文章解释了冒泡排序的核心思想和实现步骤,通过相邻元素的比较和交换,逐步将较大的元素移动到数组的末尾。接着,文章深入探讨了快速排序的多种实现方法,包括霍尔法、前后指针法、三路划分法和非递归版本,每种方法都有其特定的适用场景和优缺点。

czy02的博客 1436

【数据结构】快速排序详解

超级详细的快速排序,看完即可手撕快排!

weixin_69519040的博客 7887
上一篇: 数据结构--冒泡排序
下一篇: 数据结构--双向冒泡排序
Nim的辙迹
博客等级 码龄10年 17粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值