归并排序(递归和非递归)

非递归归并排序详细分析 非递归归并排序详细分析,Java实现. 非常详细,基本上可以看明白 立即下载

学习之后,自己练习手写一下排序算法,加深印象

原理:假设初始序列含有n个记录,则可以看做是n个有序的子序列,每个子序列的长度为1,然后两两归并,得到n/2个长度为2或1的子序列;再两两归并,如此重复,最终得到一个长度为n的有序序列

递归方法

#define MAXSIZE 10
#include <stdio.h>
struct a{
    int array[MAXSIZE];    //数组用于存储排序数据 
    int length;           //用于存储数组长度信息, 
};

void init_array(struct a *L){   //初始化数组 
    int i;
    L->length = MAXSIZE-1;
    for (i=1;i<=L->length;i++){     //索引为0的项留作备用 
        scanf("%d",&(L->array[i]));
    }
}

void swap(struct a *L,int i,int j){   //交换数组项 
    int temp = L->array[i];
    L->array[i] = L->array[j];
    L->array[j] = temp;
}

void print(struct a *L){    //打印数组项 
    int i;
    for (i=1;i<=L->length;i++){
        printf("%d ",L->array[i]);
    }
}

void Merge(struct a *L, int first, int middle, int last){   //将L->array中的两个子序列合并 
    int Index1 = first, Index2 = middle+1, mergeIndex = first;   //Index1和Index2分别为两个子序列的索引,mergeIndex为合并后序列的索引 
    int temp[L->length+1];    //创建临时序列,用作存储合并后的值 
    int i;

    for(; Index1<=middle && Index2 <=last; mergeIndex++){   //依次对比两个子序列的值,并将较小的值填入临时序列中,当一个子序列没有值可以比较时,终止 
        if (L->array[Index1] < L->array[Index2]){
            temp[mergeIndex] = L->array[Index1++];
        }else{
            temp[mergeIndex] = L->array[Index2++];
        }
    }

    if (Index1 <= middle){      //将剩下的第一个子序列填入到临时序列 
        for (; Index1 <= middle; Index1++){
            temp[mergeIndex++] = L->array[Index1];
        }
    }

    if (Index2 <= last){    //将剩下的第二个子序列填入到临时序列 
        for (; Index2 <= last; Index2++){
            temp[mergeIndex++] = L->array[Index2];
        }
    }

    for (i=first; i<mergeIndex; i++){  //将临时序列的值复制给L->array 
        L->array[i] = temp[i];
    }

}

void MSort(struct a*L, int first, int last){   //first和last分别为序列第一项和最后一项的索引
    int middle;                
    if (first < last){    //当first和last相等时,即子序列只有一个数时,不必再继续划分子序列了 
        middle = first + (last-first)/2;  //作为中点坐标,平分整个序列 
        MSort(L,first,middle);      // 归并索引从first到middle的子序列 
        MSort(L,middle+1,last);     //归并索引从middle+1到last的子序列 
        Merge(L,first,middle,last);  //将两个子序列合并 
    }
}

void MergeSort(struct a *L){   //排序算法 
    MSort(L,1,L->length);
} 

int main(){
    struct a List;
    init_array(&List);
    printf("排序前:");
    print(&List);
    MergeSort(&List);
    printf("\n排序后:");
    print(&List);
    return 0;
}

这里写图片描述
两个子序列的合并过程如上图所示,首先比较10和20这两个数,10比较小,则将其填入临时序列的第一项,再比较30和20这两个数,20比较小,则将20填入临时序列的第二项,如此比较下去,直到一个子序列到头,此时将另一个子序列剩下的,没有比较的值依次填入临时序列,最终得到合并完成的临时序列,再将其赋值给原始序列即可。
这里写图片描述

非递归算法

#define MAXSIZE 10
#include <stdio.h>
struct a{
    int array[MAXSIZE];    //数组用于存储排序数据 
    int length;           //用于存储数组长度信息, 
};

void init_array(struct a *L){   //初始化数组 
    int i;
    L->length = MAXSIZE-1;
    for (i=1;i<=L->length;i++){     //索引为0的项留作备用 
        scanf("%d",&(L->array[i]));
    }
}

void swap(struct a *L,int i,int j){   //交换数组项 
    int temp = L->array[i];
    L->array[i] = L->array[j];
    L->array[j] = temp;
}

void print(struct a *L){    //打印数组项 
    int i;
    for (i=1;i<=L->length;i++){
        printf("%d ",L->array[i]);
    }
}

void Merge(struct a *L, int first, int middle, int last){   //将L->array中的两个子序列合并 
    int Index1 = first, Index2 = middle+1, mergeIndex = first;   //Index1和Index2分别为两个子序列的索引,mergeIndex为合并后序列的索引 
    int temp[L->length+1];    //创建临时序列,用作存储合并后的值 
    int i;

    for(; Index1<=middle && Index2 <=last; mergeIndex++){   //依次对比两个子序列的值,并将较小的值填入临时序列中,当一个子序列没有值可以比较时,终止 
        if (L->array[Index1] < L->array[Index2]){
            temp[mergeIndex] = L->array[Index1++];
        }else{
            temp[mergeIndex] = L->array[Index2++];
        }
    }

    if (Index1 <= middle){      //将剩下的第一个子序列填入到临时序列 
        for (; Index1 <= middle; Index1++){
            temp[mergeIndex++] = L->array[Index1];
        }
    }

    if (Index2 <= last){    //将剩下的第二个子序列填入到临时序列 
        for (; Index2 <= last; Index2++){
            temp[mergeIndex++] = L->array[Index2];
        }
    }

    for (i=first; i<mergeIndex; i++){  //将临时序列的值复制给L->array 
        L->array[i] = temp[i];
    }

}


void MergeSort(struct a *L){   //排序算法 
    int step;             //每个子序列的长度
    int i;
    for (step=1; step<L->length; step*=2){  //子序列的长度随着归并的进行,以2为倍速增大 
        for (i=1; i<=L->length/(2*step); i++){   //i为将整个序列划分为包含两个子序列的组数 
            Merge(L,(i-1)*step*2+1,i*step*2-step,i*step*2);   //将两两子序列进行合并 
        }
        if (L->length/(2*step) == 0){    //当不足以再划分子序列时
            Merge(L,1,step,L->length);   //合并最后两个子序列 
        }
    }

} 

int main(){
    struct a List;
    init_array(&List);
    printf("排序前:");
    print(&List);
    MergeSort(&List);
    printf("\n排序后:");
    print(&List);
    return 0;
}

非递归归并排序做法直接了当,从最小的序列开始归并直至完成。不需要像递归归并算法那样,需要先拆分递归,再归并退出递归,节省了时间和空间
这里写图片描述

算法时间复杂度:O(nlogn)

python基本算法之实现归并排序(Merge sort) 主要给大家介绍了关于python基本算法之实现归并排序(Merge sort)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 立即下载

相关推荐

归并排序的python实现:递归与非递归

递归 原理比较简单,就是有序数组的合并。 def merge(a, b): c = [] i = j = 0 while i &lt; len(a) and j &lt; len(b): if a[i] &lt; b[j]: c.append(a[i]) i += 1 else: ...

Tianlock的博客 3287

安鸾渗透测试平台-CTF系列-bash

工具Xshell 公网VPS接收反弹shell Xshell新手的下载及安装教程(超详细)_新手程序员攀爬之路的博客-优快云博客_xshell使用教程 Xshell连接后 #!/bin/bash cat ${0} while : do echo "$" read input output=`$input` 显示了一个文件,不过咋做呢?这里可能考察的是linux的语法 方法 输入bash 获得shell cat ${0} while : do .

AAAAAAAAAAAA66的博客 2689

归并排序的非递归实现——python

归并排序的非递归实现 非递归的实现一直想不明白,直到看了这位大哥的博客,茅塞顿开。非常感谢这位大哥。博客地址:博客地址 递归实现是将复杂问题一步一步细分为简单的子问题,直到最小子问题的时候,开始合并。 image.png 非递归刚好相反:从最小子问题开始一步一步解决,直到复杂的问题。 image.png 由图片可知: 第一次:我们将数组分为 8个子数组 每个数组 ...

youhuakongzhi的博客 1778

使用pytorch构建LSTM_AE模型的两种方式

2.基本LSTM_Unit计算——基本LSTM_AE结构 3.自定义LSTM_Unit计算——自定义LSTM_AE结构 1.LSTM-Encoder-Decoder模型结构及简要解析 图基本lstm计算单元

qq_37840762的博客 7274

S4 Hana SD -信贷管理 - 02

在S4版本中,客户的信用管理数据在事务代码BP维护,业务伙伴角色是UKM000。在维护过程中,我们并不能直接输入信贷控制范围这个组织架构,而是需要输入一个信用段,并且在信用段的维度下去维护相应信用管理数据。对于信用段,可以分为主信用段(信用段是0000)和其他临时信用段,我们可以通过配置把次级信用段的客户信贷负债汇总到主信用段。当然,在后台也有配置,会把信用段和信贷控制范围这两者进行关联.3.2SPROIMG > 财务供应链管理 > 信用管理 > 信贷风险监视 > 主数据 > 创建信用段。

weixin_46525734的博客 1966

归并排序的递归与非递归方式(python)

# -----------递归--------------- def merge_sort(nums): if len(nums) <= 1: return nums mid = len(nums)//2 left = merge_sort(nums[:mid]) right = merge_sort(nums[mid:]) retu...

冷咖啡离开了杯垫,来到了博客 1243

100天精通Python(可视化篇)——第89天:Bokeh库绘图可视化基础入门(参数说明+案例实战)

专栏导读 一、Bokeh是什么? 二、安装与导入 三、Bokeh接口介绍 四、创建图表 五、添加自定义渲染器 切换主题 添加图例 图例位置 图例方向 图例背景和边界 图例文本的外观 行列布局 网格布局

努力让自己发光,对的人才能迎着光而来 1万+

手把手教你归并排序(非递归)

今天,我们一起来学习归并排序的非递归算法吧! 目录 一.优势 二.实现原理 三.代码实现 一.优势 相比于递归算法,归并排序的非递归算法不用多次调用同一个函数,不会向递归算法一样因为函数嵌套调用次数太多而造成栈溢出。 二.实现原理 其实,相比于递归的算法,非递归与之不同点就一个:在递归中我们通过递归到最底层(即两个数一组)进行排序,而非递归则是直接把数组分成两个数一组进行排序,这边排序完之后,再把数组分成四个一组排序,直到整个数组被分成两组,排序后就结束。 ...

weixin_61857742的博客 5373

手把手教你学Simulink——基于经典滤波器设计的场景:低通滤波器(LPF)的设计与频率响应仿真

滤波器是信号处理领域中非常重要的组件,用于去除不需要的频率成分或者提取有用的频率信息。低通滤波器(Low-Pass Filter, LPF)允许低于某个特定截止频率的信号通过,同时衰减高于该截止频率的信号。在本教程中,我们将学习如何使用设计一个简单的模拟低通滤波器,并对其频率响应进行仿真分析。本文介绍了如何使用设计一个简单的模拟低通滤波器,并通过仿真展示了其应用效果。通过这样的实践,您不仅能够加深对滤波器工作原理的理解,还能掌握利用Simulink进行滤波器设计与仿真的基本技能。

MHD0815的博客 5720

软件崩溃时Visual Studio中看不到有效的调用堆栈,使用Windbg动态调试去分析定位

当在Visual Studio中遇到软件崩溃且无法查看详细调用堆栈时,可以使用Windbg进行动态调试去分析定位问题。

dvlinker的技术专栏 1万+

归并排序python非递归_【Python排序搜索基本算法】之归并排序

[归并排序(Merge sort)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。 最差时间复杂度:O(nlogn) 最优时间归并排序最令人兴奋的特点是:不论输入是什么样的,它对N个元素的序列排序所用时间与NlogN成正比。代码如下:[归并排序算法1.算法原理归并排序算法是分治法(Divide-and-Conquer)的典型应...

weixin_30977095的博客 306

YOLOv11涨点改进 | 全网独家创新,注意力改进篇 | TGRS 2025 | 引入APCM自适应像素级协同机制,动态增强目标区域、抑制背景噪声,助力YOLOv11做红外小目标检测有效涨点

本文提出了一种基于APCM模块改进的YOLOv11网络模型,通过像素级局部-全局协同建模增强目标特征并抑制背景噪声。APCM模块采用轻量化设计,无需复杂参数,能有效提升小目标检测性能。文章详细介绍了APCM的原理、优势及其在红外小目标检测中的应用,包括模块结构、核心代码实现和YAML配置文件修改方法。实验结果表明,该方法在NUAA-SIRST和IRSTD-1K数据集上优于现有技术,显著提高了复杂场景下的检测精度与鲁棒性。

Ai缝合怪、助力小伙伴高效发论文 117

归并排序(递归和非递归方法)

重点图解实现归并排序的2种方法(递归和非递归版)(含全部代码); 归并排序可以进一步被划分为分割和归并两个步骤。分割是将数组二等分,得到的子数组继续二等分,直到每个子数组只剩下一个元素为止。而归并则是不断将原本属于同一个数组的两个子数组归并成一个新的有序表。

好作息,好心态,多锻炼,多学点,多钻研 1481

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

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

归并排序的递归和非递归

归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有 序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。 关键词:分解、归并、分治、物理上数组、逻辑上二叉树 思路: 把长度为n的输入序列分成两个长度为n...

紫阳战君的博客 1133

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

归并排序递归方法和非递归方法详解

归并排序递归方法和非递归方法详解

Xpccccc的博客 1972

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

快速排序和归并排序中一趟的理解(递归和非递归)

引:2019年408中数据结构一道考察快速排序的选择题 定位:这道题在考察快速排序中一趟的概念。 思路: 回想教材(《数据结构》严)里对一趟的定义 算法描述: 可见第一张图的过程实际上是递归形式实现的快排第一次调用Partition函数产生的结果。 然而,我们知道,如果第一次选中的pivot处在了待排序元素最终结果中的中间位置。那么接下来的处理也是递归进行的, 如图(a)所示,在第一次调用Patition函数后,元素49被放到了最终位置,之后对49左侧位置元素调用Qsort进行处理

Kazzan的博客 1万+

归并排序的递归和非递归方法总结

归并排序的递归和非递归算法

a815331793的专栏 4569

排序算法:归并排序(递归和非递归)

归并排序使用递归的实现方法以及在递归版本中的小区间优化版本,归并排序的非递归实现(两个改进的版本)!

Yikefore的博客 2957

排序算法进阶——归并排序【详细图解,递归和非递归】

再比较两个序列的第一个元素的大小,将小的那一个元素放在申请的空间的第一个【假设排升序】,再让放入的那个元素之后的一个元素与那个第一次比较时没放入的元素比较,再把小的那一个放入申请空间的第二个位置上…归并算法一般应用于合并两个已经有序的序列,使合并后的序列也有序,是一个时间复杂度为O(N)的算法,不过一般要借助两个要排序的序列的元素个数个额外的空间。归并排序采用分治策略,将序列递归地分成短序列,然后将各个有序的短序列合并成一个有序的长序列,不断合并直到原序列全部排好序。

2301_80058734的博客 3312

快速排序的非递归写法和优化,归并排序的非递归写法

非递归和优化快速排序非递归优化1、 优化基准值的选择2、减少递归次数3、结合堆排序 快速排序 非递归 本质上需要用非递归模拟递归,借助一个栈 import java.util.Arrays; import java.util.Stack; public class quickSort { public static void quickSort(int[] array){ //栈中保存的元素相当于当前要进行partition操作的范围下标 Stack<Inte

char_m的博客 502

归并排序的递归和非递归实现

归并排序递归和非递归实现

Latecomessnow的博客 523

【排序知多少】归并排序(递归和非递归实现)

归并排序思路 1、将待排序元素一分为二 2、对于左半边和右半边元素分别再次进行拆分,直到无法再拆 3、把拆分过的元素进行重新排序并且合并 4、合并之后最终的数组即为排序之后的数组 归并排序理解 归并排序适用了完全二叉树排序的想法,将带排列数组逐层均分,尽可能分成完全二叉树的形式,再把每组查分的元素从最底层开始,逐层向上的合并起来,直到再次和成一个有序的数组,即完成整个排序过程。 归并...

不了痕的博客 1406

归并排序三种实现方法(递归、非递归和自然合并排序)

1.      递归实现归并排序 1)     基本思想: 将待排元素分成大小大致相同的2个子集,分别对2个子集合进行排序,最终将排好序的子集合合并 就会得到一个排好序的集合 即为所求 设归并排序的当前区间是R[low..high],分治法的三个步骤是: ① 分解:将当前区间一分为二,即求分裂点  ② 求解:递归地对两个子区间R[low..mid]和R[mid+1..high] 进行归并

Christine的专栏 1万+

归并排序(递归和非递归写法)

本篇博客主要介绍排序算法中的归并排序(MergeSort),包括递归和非递归的实现。 代码实现:C语言

分享学习方面的一些笔记 423
上一篇: Javascript学习笔记7 DOM2和DOM3
下一篇: 快速排序
PAT-python-zjw
博客等级 码龄10年 381粉丝 408原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值