快速排序递归与非递归实现

快排伪代码 一、单向扫描法: #include<stdio.h> #include<iostream> using namespace std; void quickSort(int arr[],int p,int r){ int partition(int arr[],int p,int r); if(p<r){ int q=partition(arr,p,r); quickSort(arr,p,q-1); quickSort(arr,q+1,r); } } v 阅读详情

算法概述:

分而治之。

选出一个元素作为主元,它可以把集合中的元素分成两部分,一部分都是大于它的,一部分都小于它的。

我们先用伪代码描述。

void QuickSort(ElementType A[],int N)
{
   //如果只剩一个元素了直接返回
   if(N<2) return;
   pivot=从A[]选出一个主元;
   将S={ A[]\pivot }分成两个独立的子集;
     A1={ a∈S | a≤ pivot }和A2={ a∈S | a≥ pivot};
   A[]=QuickSort(A1,N1)∪ pivot ∪ QuickSort(A2,N2);
}

选主元

选主元一定要慎重。如果是下面这种情况
共有5个元素,1 2 3 4 5
选1作为主元那么比1小的为空,比1大的有4个。这样进行的快排是一点也不快的。
取头,中,尾的三个元素比较,选出中间大小的元素作为主元

ElementType Median(ElementType A[],int left, int right)
{
   int center=(left+right)/2;
   if(A[left]>A[center])
   {
       Swap(&A[left],&A[center]);
   }
   if(A[left]>A[right])
   {
      Swap(&A[left],&A[right]);
   }
   if(A[center]>A[right])
   {
      Swap(&A[center],&A[right]);
   }
   //将pivot放到right-1的位置,我们知道A[left]肯定小于主元A[right]肯定大于主元
   //只需要考虑A[left+1]~A[right-2]
   Swap(&A[center],&A[right-1]);
   return A[right-1];
}

子集划分

6是主元,被放在最右端。
这里写图片描述
当i对应的元素大于主元,j对应的元素小于主元,停下来,进行交换
这里写图片描述

当i>j时,停止交换。并把主元与i位置元素交换。选出来的主元,在完成一次子集划分后,主元会被放到最终应在的位置。

这里写图片描述

小规模数据的处理

因为使用了递归,对于小规模的数据,可能快排还没有如插入排序的简单排序快。
所以我们可以设定一个阈值Cutoff,当递归时的数据规模足够小,我们就停止递归了,直接调用简单排序。

递归

代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
int median(int* arr, int left, int right){
    int center = (left + right) / 2;
    if (arr[left] > arr[center]){
        swap(arr[left],arr[center]);
    }
    if (arr[left] > arr[right]){
        swap(arr[left],arr[right]);
    }
    if (arr[center] > arr[right]){
        swap(arr[center],arr[right]);
    }
    swap(arr[center],arr[right-1]);
    return arr[right - 1];
}
void insertSort(int* arr,int n){
    int tmp;
    for (int i = 1; i < n; i++){
        tmp = arr[i];
        int j;
        for (j = i; j>0 && arr[j - 1] > tmp; j--){
            arr[j] = arr[j - 1];
        }
        arr[j] = tmp;
    }
}
int cutoff = 5;
void quickSort(int* arr,int left,int right){
    if (cutoff <= (right - left)){
        int pivot = median(arr,left,right);
        int i = left;
        int j = right - 1;
        while (1){
            while (arr[++i] < pivot){
            }
            while (arr[--j]>pivot){
            }
            if (i < j){
                swap(arr[i],arr[j]);
            }
            else{
                break;
            }
        }
        swap(arr[i],arr[right-1]);
        quickSort(arr,left,i-1);
        quickSort(arr,i+1,right);
    }
    else{
        insertSort(arr+left,right-left+1);
    }
}
int main()
{
    int arr[20] = {20,19,18,10,17,16,15,14,13,9,5,6,7,8,1,2,3,11,4,12};
    quickSort(arr,0,19);
    system("pause");
    return 0;
}

非递归,手动利用栈来存储每次分块时快排的起始点。这种写法的执行效率低于递归算法。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<stack>
using namespace std;
struct Node{
    int low;
    int high;
};//存放待排子列的区间
void insertSort(int* arr, int n){
    int tmp;
    for (int i = 1; i<n; i++){
        tmp = arr[i];
        int j;
        for (j = i; j>0 && arr[j - 1] >= tmp; j--){
            arr[j] = arr[j - 1];
        }
        arr[j] = tmp;
    }
}
int median(int* arr, int left, int right){
    int center = (left + right) / 2;
    if (arr[left] > arr[center]){
        swap(arr[left],arr[center]);
    }
    if (arr[left] > arr[right]){
        swap(arr[left],arr[right]);
    }
    if (arr[center] > arr[right]){
        swap(arr[center],arr[right]);
    }
    swap(arr[center],arr[right-1]);
    return arr[right - 1];
}
int partition(int* arr,int left,int right){
    int pivot = median(arr,left,right);
    int i = left;
    int j = right - 1;
    while (i<j){
        while (arr[++i] < pivot){}
        while (arr[--j] > pivot){}
        if (i < j){
            swap(arr[i],arr[j]);
        }
        else{
            break;
        }
    }
    swap(arr[i],arr[right-1]);
    return i;
}

void quickSort(int* arr,int left,int right){
    stack<Node> s;
    int pivot;
    Node node;
    node.low = left;
    node.high = right;
    s.push(node);
    while (!s.empty()){
        node = s.top();
        s.pop();
        pivot = partition(arr,node.low,node.high);
        if (pivot - 1 > node.low){
            Node node1;
            node1.low = node.low;
            node1.high = pivot - 1;
            s.push(node1);
        }
        if (pivot + 1 < node.high){
            Node node2;
            node2.low = pivot + 1;
            node2.high = node.high;
            s.push(node2);
        }
    }
}
int main()
{
    int arr[20] = { 20, 19, 18, 10, 17, 16, 15, 14, 13, 9, 5, 6, 7, 8, 1, 2, 3, 11, 4, 12 };
    quickSort(arr, 0, 19);
    system("pause");
    return 0;
}
快速排序(Quick Sort)的递归与非递归实现 原理 快速排序采用分治的策略,是一个不稳定的排序算法。其基本思想是:将待排序的序列分为三个部分,分别为基准arr[p],arr[0:p-1]与arr[p+1:n],通过操作使得arr[0:p-1]中的所有元素都比基准arr[p]小(或等),而arr[p+1:n]中的所有元素都比基准arr[p]大(或等),然后递归地对arr[0:p-1]与arr[p+1:n]执行同样的操作,直到序列不可再被分割,... 阅读详情

相关推荐

快速选择非递归与递归算法实现

快速选择非递归与递归算法实现

常见排序算法之快速排序原理及递归非递归版本代码

目录快速排序原理复杂度分析选择排序代码1.递归版本2.非递归版本测试及结果测试结果 快速排序原理 之前介绍过三种基础的排序算法以及它们之间的比较:冒泡排序,选择排序和直接插入排序,我们知道它们的时间复杂度都是O(n2),那是不是所有的算法的时间复杂度都是O(n2)呢?当然不是!下面我们介绍的快速排序之所以叫快速排序就是因为目前已有的排序算法中,快速排序的时间复杂度是最小的。 快速排序的基本思想:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分小,则可以分别对这两部分记录继续进行排

JM-Nie的博客 777

数学不好是原罪——高等数学笔记(汇总版)

这是本人之前考研的高数手写笔记,工科学硕数一考了146(满分150),笔记有一定参考价值,欢迎大家收藏借鉴。

xuuyann 5万+

快速排序----非递归

我们以前写快排都是写递归的。 #include #include #include #include #include #include using namespace std; template int partition(vector &vec,int low,int high) { Comparable pivot=vec[low]; //任选元素作为轴,这里选首元素

Life Coding 2955

FPGA新手必看:GT收发器示例工程从零搭建到调试(附眼图测试技巧)

本文详细解析了FPGA中GT收发器示例工程的从零搭建到调试全流程,特别适合新手入门。内容涵盖工程创建、GT Wizard IP核配置、示例工程代码解析及眼图测试技巧,帮助读者快速掌握高速串行通信的关键技术。重点介绍了眼图测试的硬件连接规范和常见问题排查方法,提升调试效率。

weixin_29248313的博客 402

从HPPC测试到Simulink仿真:锂电池二阶RC模型参数辨识完整流程

本文详细介绍了从HPPC测试到Simulink仿真的锂电池二阶RC模型参数辨识完整流程。通过HPPC测试获取实验数据,利用Simulink的Parameter Estimation工具进行参数辨识和模型验证,为BMS算法开发提供精确的电池模型。文章涵盖测试设计、数据预处理、模型搭建及工程应用等关键步骤,助力新能源电池研发。

whisky的博客 689

快速排序非递归优化c语言,快速排序算法的几种实现的性能对比:递归实现和非递归实现 | 王成文的个人站点...

本文对快速排序的几种实现进行速度分析。首先根据《算法导论》p85提供的伪代码有如下快速排序的实现:0102030405060708091011121314151617181920212223242526int partition(int *a,int start,int end){int x = a[end];int i = start-1;int j=start;for (int j = sta...

weixin_42501785的博客 524

​​WPF入门与XAML基础:从零开始构建你的第一个WPF应用​

本文是WPF入门系列首篇,详细介绍WPF的核心优势与开发环境搭建。通过创建第一个WPF项目,解析MainWindow.xaml和App.xaml等关键文件,深入讲解XAML基础语法,包括属性赋值、命名空间和事件绑定。最后通过按钮点击交互示例演示WPF应用程序运行流程,帮助开发者快速上手WPF开发,为后续学习布局系统和数据绑定等高级主题奠定基础。

Hare的博客 3349

快速排序的递归和非递归解法

快速排序非递归的实现方法借用堆栈实现,栈和队列是消除递归的常用方法。using System; using System.Collections.Generic; using System.Text; namespace QuickSort { class Program { public static void Main(string[] args) { int[] arr = { 4, 3

sulliy的专栏 1534

YOLOv13改进策略【Neck】| GSConv+Slim Neck:混合深度可分离卷积和标准卷积的轻量化网络设计

实时检测器架构的轻量级设计GsConv。

Limiiiing的博客 102

使用 Simulink + Simscape Electrical 搭建一个电力系统模型,并通过改变负荷参数来模拟负荷波动

摘要:本文介绍了使用Simulink进行电力系统稳态分析的方法,重点研究负荷波动对系统的影响。通过构建包含同步发电机、变压器、输电线路和负荷模块的三节点系统模型,详细演示了建模过程,包括添加模块、搭建主电路拓扑和设置仿真参数。文章提供了两种负荷波动模拟方法,并分析关键指标如频率稳定性、电压水平和功率平衡。最后给出优化建议如引入储能系统、优化调度策略等,为电力系统规划、运行管理提供实用参考。

amy_mhd的博客 265

快速排序的递归实现和非递归实现

找比基准值小的值,找到后停在此位置,然后指向首位置的left开始向右走,找到比基准值大的值,找到后停在此位置,然后将此时left和right分别指向的值进行交换,再继续刚才的找法。进入循环,然后判断cur位置的值是否小于基准值,若小于,再判断prev+1后的位置是否与此时cur不在同一位置,若是,则交换此时prev和cur位置的值。首先定义变量prev,并用其保存left的值,再定义变量cur,cur保存left+1的值,并定义基准值的位置key是left的位置。因此使用非递归形式进行排序是必要的。

2301_80667185的博客 1968

将Keil中的变量数组导出

首先,写好导出数据的函数。

小柚子的博客 1961

快速排序的非递归实现

快速排序的非递归实现

mmqqyyqqyyq的博客 962

华为数通方向HCIP-DataCom H12-831题库(多选题:121-140)

华为数通HCIP H12-831多选题

didiplus 1298

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

快速排序————非递归实现

大家有可能会想是不是因为递归非常的占用空间,我们都知道我们的局部变量是保存在栈上的我们的函数参数也是在栈上开辟的,所以说递归是不是会占用我们非常多的栈空间,同时呢我们的递归是一个非常深的一个操作,我们往往会因为一个函数递归出来的结果而去让这个函数重复调用多次去解决我们当前的问题,我们的快速排序非递归就是这样的方式,也包括之前的一个斐波那契数列使用递归去解决都是使用了栈;这样的方法在递归的深度不大的时候不会产生什么问题但是如果我们需要去处理的数据非常多,递归的深度非常深那么我们这个时候递归真的不会出现问题吗。

2201_75943325的博客 1563

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

快速排序之递归实现

快速排序的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。 简单来说就是找基准。使基准前面的数都比它大(小),后面的数都比它小(大)。再采用分治思想将问题一次次分为规模较小的子问题,通过这样进行排序,最终实现整个序列的有序。 1,找基准...

weixin_42293337的博客 1557

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

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

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

快速排序递归与非递归C++实现

快速排序基本思路 对一个给定的数组, 选择一个元素, 对数组进行整理,使得位于该元素的左边的元素都比选定元素小, 右边的部分都比选定元素大或相等; 这个整理过程叫做partition; 把partition过程递归的应用到选定元素的左侧和右侧的子数组,直到子数组少于2个元素,递归结束; 整个数组变为有序的; 所以问题转变成partition的实现, 给定一个数组,选一个数组中的元素, 如何整理使得...

葡萄老君 1235

快速排序的递归与非递归实现

快速排序 取一个标杆元素pivot 将小于pivot的元素放左边,大于pivot的元素放右边 对第二步产生的左右两个区间进行相同操作,直到排序完成 递归实现 递归实现易于理解,其主要思想就是通过递归来不断对左右两个子区间进行快速排序直到排序完成。 public int[] quickSort(int[]arr){ quickSort(arr,0, arr.len...

Juicewyh的博客 1581

递归实现快速排序

所有查找中Hash查找效率最高,在所有排序中,快速排序的效率也是最高的。 采用递归函数的方法来实现快速排序! 先把递归框架搭建起来!!! 递归框架,首先确定终止条件,那就是当 low >= hight的时候!!! 然后确定递归操作,已经知道每一次拆分会产生两个区间,又要对两个区间各自进行拆分! 对于拆分操作,输入的是列表和low、hight,返回的是拆分好后,基准值的下标!!! 对于拆分出来的两个区间的再次拆分,根据上次拆分的基准值下标来确定这两个区间的low、hight 在拆分的时候,low和hi

肥宅快乐博客 2122

快速排序递归实现和非递归实现(详解)

内容:快速排序的递归/非递归实现代码及注解,思路详解,以及快速排序的优化

weixin_73142957的博客 744

快速排序C++实现(递归,非递归)

#include #include #include using namespace std; int quickSortRecusive(vector& data,int i,int j) { int l = i; int r = j; int x = data[r]; //基准 if(l >= r) return l; while(l < r){ while(l <

王飞平的博客 6136

快速排序的递归实现

第八篇–快速排序的递归实现 #include<iostream> #include<stdlib.h> using namespace std; int a[5] = { 2,1,7,4,9 }; void Swap(int a[],int i ,int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } int...

qq_40395913的博客 270

快速排序实现之递归与非递归

一、算法思想:    快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)。设当前待排序的无序区为R[low..high],利用分治法可将快速排序的基本思想描述为:在R[low..high]中任选一个记录作为基准(Pivot),以此基准将当前无序区划分为左、右两个较小的子区间R[low.....

Try My Best! 257
上一篇: 归并排序的3种实现
下一篇: char* p与char p[]
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址