博客
关于我
分治法之合并排序(2021/1/23)
阅读量:685 次
发布时间:2019-03-17

本文共 870 字,大约阅读时间需要 2 分钟。

问题引入

在这里插入图片描述

代码实现

#include
#include
using namespace std;struct Data{ int flag;};void MergeFunction(struct Data*list,int low,int middle,int high){ //申请辅助空间 int size=high-low; struct Data*space=(struct Data*)malloc(sizeof(struct Data)*size); int i=low,j=middle+1; int now=0; while(i<=middle||j<=high){ if(i<=middle&&j<=high){ //比较i j flag if(list[i].flag
=high){ //=说明只有一个元素不用排序 return; } int middle=(low+high)/2; //对middle左边排序 MergeSort(list,low,middle); //对middle右边排序 MergeSort(list,middle+1,high); //合并 MergeFunction(list,low,middle,high);}int main(int argc,char**argv){ struct Data list[5]={ { 34},{ 5},{ 2},{ 7},{ 10}}; MergeSort(list,0,4); //输出 for(int i=0;i<5;i++){ cout<<" "<

程序输出

2 5 7 10 34--------------------------------Process exited after 0.08149 seconds with return value 0请按任意键继续. . .

转载地址:http://mbshz.baihongyu.com/

你可能感兴趣的文章
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>