LintCode-31: Partition Array 及QuickSort学习

6-11 快速排序 函数quickSort完成快速排序(从小到大),其中array是需要排序的数组,start是array中排序部分的起始位置,end是array中排序部分的结束位置。函数partition完成分区操作,其中array是需要分区的数组,start是分区之前的起始位置,end是分区之前分的结束位置,该函数返回分区后基准值所在位置。 阅读详情

Partition Array类似于quickSort里面的Paritition的过程,但是有点细微区别。代码如下:

    int partitionArray(vector<int> &nums, int k) {
        int start=0, end=nums.size()-1;
        while (start<=end) { //note: <= 
            while ((start<=end) && (nums[start]<k)) start++; //note: <=  <
            while ((start<=end) && (nums[end]>=k)) end--; //note" <= >=
            if (start<=end) { //note: <=
                swap(nums[start], nums[end]);
               // start++; **//这两行代码可要可不要**
               // end--;
            }
        }
        return start;
    }

QuickSort的代码如下:

    void quickSort(vector<int>& A, int start, int end) {

        if (start>=end) return;

        int left=start, right=end;
        //1. pivot cannot be start or end, and don't choose index, we need to choose the value, as the partition is not stable, the elements' sequence is not garanteed!
        int pivot=A[start+(end-start)/2]; //if pivot=A[start] or A[end] it is easy to cause the non-even partition, or the random() will have overhad.

        //2. it is <=, not <
        while(left<=right) {   
            while(left<=right && A[left]<pivot) {
                left++;
            }

            while(left<=right && A[right]>pivot) {
                right--;
            }

            if (left<=right) {
                int temp=A[left];
                A[left]=A[right];
                A[right]=temp;
                left++;
                right--;
            }
        } 

        quickSort(A, start, right); //right moved to left
        quickSort(A, left, end); ///left moved to right
    }

    void sortIntegers2(vector<int> &A) {
        if (A.size()==0) return;
        quickSort(A, 0, A.size()-1);
    }

要注意的地方有:
1)所有地方都用left<=right。为什么呢?因为如果写成left < right,就会导致死循环(迭代版)或stack overflow(递归版)。举个例子:
nums[]={3,2,1,4,5},一开始left=0(指向3), right=4(指向5), pivot=nums[(0+4)/2]=1 (index=1),然后left指向3(不动),right指向1(–两次)。然后left和swap的元素交换,变成{1,2,3,4,5}。left和right都是1,指向2(即pivot)。此时(left < right)这个条件不满足了,会继续递归,
quickSort(A, start,right) => quickSort(A,0,1)
quickSort(A, left, end) => quickSort(A,1, 4)。这里pivot还是nums[(1+4)/2]=1,数组为{X,2,1,4,5},最后left=1指向2,right=2指向1,交换元素以后变成{X,1,2,4,5},left=2指向2,right=1指向1,然后又递归
quickSort(A,1,1)
quickSort(A,1,4) 所以导致stack overflow。

而用(left <= right)的话,上面当left=right=1时,while(left<=right)仍然满足,此时内部while()的A[left] < pivot和A[right]>pivot不满足,所以进入if(left <= right),此时原地交换,left=2,right=0,进行递归
quickSort(A,0,0), nums={1,X,X,X,X} 不会再递归了。
quickSort(A,2,4), nums={X,X,3,4,5}, start=2, end=4, pivot=nums[3]=4。
left=2指向3, right=4指向5,此时left++,right–,都=3指向4。此时left <= right仍然满足,原地交换后left++变成4,right–变成2。再递归
quickSort(A,2,2)不递归了
quickSort(A,4,4)也不递归了。

2)pivot必须是元素值而不是index,因为partition可能会交换元素,所以index就不管用了。这也是为什么quickSort是不稳定排序的原因。
3)<=pivot的在左边,>=pivot的在右边。也就是说pivot即在左边又在右边。为什么不是<的在左边,>=的在右边,或者<=的在左边,>的在右边呢?这是考虑到数组里面可能很多重复元素,比如说nums={1,1,1,1,1,1,1},如果< p的在左边,>=p的在右边,会导致左边一个都没有,右边还是n个的极端情况。
4) quickSort的话,if(left<=right)的left++和right–是必须的。
而这个LintCode的Partition Array则可要可不要。我想这个是为了防止递归overflow的缘故。
5) quickSort里面是(A[left] < pivot)和(A[right] > pivot)。而Partition Array则是(nums[start] < k)和(nums[start] >= k)。我想quickSort是要把元素分为< pivot,= pivot和> pivot三部分,而Partition Array只是分为2部分。
6) 这些是我的理解,还没有确认。
quickSort里面每次partition完之后,[start, end]里面可分为3部分:左边是[start, j], 中间是pivot,右边是[i, end]。注意这里pivot是取的数组元素,所以一定可以取到。不过什么时候会只分成2部分呢?

而上面的Partition Array里面的k不一定是数组里面的元素,每次partition完之后,数组分为2部分,[0, start] 都是小于k的数,[start+1, end]都是大于等于k的数。

EDEM基础操作指南 EDEM(Enhanced Discrete Element Method)是一种用于模拟颗粒物料行为的软件工具。它可以模拟颗粒的碰撞、流动、堆积等过程,广泛应用于颗粒物料工程、振动筛分、堆垛机械、矿山开采等领域。本文将介绍EDEM的基础操作步骤,并提供相应的源代码示例。以上是EDEM的基础操作步骤。通过创建EDEM项目、定义颗粒材料、导入几何模型、设置边界条件、创建颗粒流场和运行模拟,您可以开始模拟颗粒物料的行为并进行相应的分析和优化。请根据您的具体需求和场景进行相应的参数设置和功能调用。 阅读详情

相关推荐

无法找到 Adobe PDF资源文件。“Acrobat PDFMaker“您必须具有”管理员”权限方可安装这些文件...错误怎么解决

本文的内容是无法找到 Adobe PDF资源文件。“Acrobat PDFMaker“您必须具有”管理员”权限方可安装这些文件...错误的解决办法。

西岸贤 1万+

排序算法 -快速排序

排序算法-快快快速排序

shiyue 1234

思科模拟器Cisco Packet Tracer交换机的管理配置

一、交换机的管理方式:带内管理和带外管理 通过交换机的 Console 端口管理交换机属于带外管理;这种管理方式不占用交换机的网络端口,第一次配置交换机必须利用 Console 端口进行配置。 通过 Telnet、拨号等方式属于带内管理。 二、控制台配置思科交换机 1.新建拓扑图 新建拓扑,选择console线连接交换机和PC,一端接交换机的console口,一端接PC的RS 232 口(...

qq_37992321的博客 2万+

快排中的Partition函数

快排的递归形式 快排算法的程序结构: def quicksort(nums, l, r): if l >= r: return k = partition(nums, l, r) quicksort(nums, l, k - 1) quicksort(nums, k + 1, r) 其中partition函数输入参数是一个数组nums和两个位...

东明山庄 4474

【上海大学计算机组成原理实验报告】一、数据传送实验

将58H写入A寄存器//将6BH写入W寄存器//将C3H写入R1寄存器//将C3H写入R1寄存器//了解实验仪器数据总线的控制方式//掌握数据传送的基本原理//掌握各寄存器的结构、工作原理及其控制方法//数据传送实验

HackerKevon的博客 3511

【Spreadsheet】香港规范结构设计电子表格库开发方案

深入理解香港本地规范特点和要求结合国际先进标准(Eurocode) 的技术内容考虑香港地理环境特殊性 (台风、地质等)建立完善的质量控制体系保持持续更新维护的能力。

hmywillstronger的博客 1364

C/C++ 实现在快速排序Quick Sort中的三种分区方式

神说, 要有光. 于是就有了光. 神说要有快排, 于是就有了快排.快速排序Quick Sort的发明者 托尼 霍尔 是1980年的图灵奖得主. 快速排序就是他发明的. 当时发明的背景是: 由于霍尔要高效地对俄语词汇进行排序以优化翻译程序, 而当时的排序算法(如冒泡, 插入排序)效率较低, 平均时间复杂度为 O(n2). 霍尔驾驶室分治法 (Divide and Conquer)思想启发, 提出了基于 "分区" (Partition) 的递归排序方法. (这段AI获取)

losangelx的博客 1148

Ubuntu20.04下运行VINS系列:VINS-Mono、VINS-Fusion和GVINS

安装好了VINS-Mono所需的ROS noetic、Eigen3.4.0、Ceres2.10和OpenCV4.2.0等。VINS-Mono是在ROS melodic以及Ceres1的环境下开发的,因此需要适配高版本的环境。

zardforever123的博客 2万+

图解排序算法及实现——快速排序 (Quick Sort)

接下来让我们看看大名鼎鼎的快速排序,光名字就觉得牛哄哄。 那么具体怎么讲待 1.单路排序 图例如下 比照了示意图,很容易写出代码: def QuickSort(arr): n = len(arr) _quickSort(arr,0,n-1) def _quickSort(arr,l,r): if l &amp;amp;amp;amp;amp;amp;amp;gt;= r: re...

张渊猛的博客 2万+

【RT-DETR有效改进】利用MobileNetV3替换Backbone(轻量化网络结构,提点)

本文给大家带来的改进机制是,其主要改进思想集中在结合硬件感知的网络架构搜索(NAS)和NetAdapt算法,以优化移动设备CPU上的性能。它采用了新颖的架构设计,包括反转残差结构和线性瓶颈层,以及新的高效分割解码器Lite Reduced Atrous Spatial Pyramid Pooling(LR-ASPP),以提升在移动分类、检测和分割任务上的表现。实验表明,MobileNets在资源和准确性的权衡方面表现出色,并在多种应用(如对象检测、细粒度分类、面部属性识别和大规模地理定位)中展现了其有效性。

Snu77的博客 2751

【目标检测】全局上下文感知的详解

目标检测中的全局上下文感知(Global Context Awareness)​是指模型在定位和识别目标时,能够有效利用图像中全局或大范围的上下文信息(如场景语义、目标间关系、空间布局等),而不仅仅依赖局部特征。这种能力显著提升了检测的鲁棒性,尤其是在复杂场景(如遮挡、小目标、密集目标)中表现突出。

浩瀚之水的专栏 650

lintcode31 - Partition Array - medium

Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:* All elements < k are moved to the left* All elements >= k are moved to the rightRe...

weixin_30819163的博客 132

FPGA 学习笔记:Vivado 2020.2 MicroBlaze MIG 测试 DDR3 篇四

FPGA 学习笔记:Vivado 2020.2 MicroBlaze MIG 测试 DDR3 篇四

坚韧与专注 2164

1、直接插入排序InserTSort()

//直接插入排序InserTSort()

风暴计划 1381

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

从partition到Quicksort

本文主要讨论quicksort和寻找第k大元素的两个相关问题。 力扣连接https://leetcode-cn.com/problems/sort-an-array/【排序算法精华3】快速排序 (上)_哔哩哔哩_bilibili"五点七边-算法学习" QQ交流群:539835344快速排序(英语:Quicksort),简称快排,是一种排序算法,最早由东尼·霍尔提出。正如它的名字所暗示的那样,快速排序的最大优点在于它的速度非常快。在实现的比较好的时候,快速排序往往比归并排序、堆排序等优秀的排序算法更快...

m0_47096428的博客 453

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

LintCode 463: Sort Integers (QuickSort和MergeSort 经典模板!!!)

Sort Integers 中文English Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm. Example Example 1: Input: [3, 2, 1, 4, 5] Output:...

roufoo的博客 1814

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

快速排序quick_sort(python的两种实现方式)

排序算法有很多,目前最好的是quick_sort:unstable,spatial complexity is nlogN. 快速排序原理 python实现 严蔚敏的 datastruct书中有伪代码实现,因为Amazon面试需要排序,所以用python实现了。 两种实现方法,功能一致,效率没测,请高手留言 第一种实现 标准算法,严蔚敏书中的伪代码实现...

weixin_30814319的博客 653

算法导论 - QuickSort 快速排序 C++实现

算法导论的快速排序还和一般书上的快速排序是有点不一样的。 当然书习题也给出了一般快速排序的方法,其分区函数学名叫Hoare partition。 书本介绍的排序可以用图看的很清晰: 然后配合C++程序,就不需要废话就能明白了: //C++'s array range should be [low, up], the same as [low, up+1) int partition...

架构设计 342

快速排序(QuickSort)算法介绍

算法简介快速排序(Quicksort)是对冒泡排序的一种改进算法。由C. A. R. Hoare在1960年提出。该算法使用广泛、效率很高,是最重要的排序算法之一。该算法的实现基本可分为以下几步:

wthfeng的专栏 6万+

快速排序quick sort

.. public class QuickSort { public static int partition(int[] ints, int left, int right){ int base = ints[left]; while(left < right){ while(left = base){ right--; } ints[left] = i

jjavaboy的专栏 1639

【算法】标准排序之快排( Quicksort )

1.简介 快速排序是很经典的分治算法,大概的算法思路就是将大数组划分partition为两个子数组,左数组整块小于右数组(max(left)<min(right)),然后递归处理左子数组和右子数组,如下图所示: 2.算法实现 2.1 quickSort递归 Divide 阶段(partition): 挑一个数当作pivot基准, 把数组分割...

opi 741

QuickSort -- 快速排序(C++)

方案一     每次采用最后一个元素作为 pivot,代码如下: template int partitionArray(T arr[], int low, int high) { T pivot = arr[high]; int i = low - 1; for(int j = low; j < high; ++j) if(arr[j] <= piv

zhangpiu的专栏 1108

Quick Sort的两种实现方法---Python

1.思想: 取一个参考值,让序列所有的元素排序后,左侧子序列的值都比参考值小,右侧子序列的值都比参考值大。 分别对左侧和右侧的子序列做1中的递归操作。 基于上述思想,常用的实现方法有两种,一种是简单、容易理解的Lomuto scheme,另一种是效率更高的Hoare scheme。下面的代码参考自维基百科的伪代码实现,不清楚的可以移步维基,仔细研究。 2.Lomuto par...

weixin_37621790的博客 1111

快速排序(Quick sort)

Java语言实现快速排序

程序袁的优快云 343

快速排序法-Quick Sort

快速排序法-Quick Sort 【算法思想】 (1)在当前数组中选择一个数为基点,使它处于排好序的位置,并且这个数左边的数小于它,右边的数大于它。 (2)分别对这个基点左右两边的数进行相同操作。 【Partition】 (1)通常选取数组最左边的数作为分界点元素,这里我们标记为l,然后我们开始遍历剩余的数组,将其分为小于v的部分和大于v的部分,这两个部分的分界点我们用j来表示。当前访问的元素...

Blog of a programmer who wants to open a cafe 239

快速排序(Quicktsort)之Java实现

快速排序算法介绍 快速排序和归并排序都使用分治法来设计算法,区别在于归并排序把数组分为两个基本等长的子数组,分别排好序之后还要进行归并(Merge)操作,而快速排序拆分子数组的时候显得更有艺术,取一个基准元素,拆分之后基准元素左边的元素都比基准元素小,右边的元素都不小于基准元素,这样只需要分别对两个子数组排序即可,不再像归并排序一样需要归并操作。基准元素的选取对算法的效率影响很大,最好的情况是两

永无止境,上下求索 4万+
上一篇: LintCode-143: Sort Colors II
下一篇: LintCode-607: Two Sum III
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值