当前位置: 首页 > news >正文

网站海外推广公司新网域名注册官网

网站海外推广公司,新网域名注册官网,南昌做网站市场报价,建设微信商城网站制作目录 一 基本思想 二 代码实现 三 非递归归并排序 一 基本思想 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并&#xff…

目录

一 基本思想

二 代码实现 

三 非递归归并排序 


一 基本思想

归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有 序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。 归并排序核心步骤

二 代码实现 

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>void _MergeSort(int* a, int* tmp, int begin, int end)
{if (begin >= end){return;}int mid = (begin + end) / 2;_MergeSort(a, tmp, begin, mid);_MergeSort(a, tmp, mid + 1, end);//归并int begin1 = begin, end1 = mid;int begin2 = mid + 1, end2 = end;int index = begin;while (begin1 <= end1 && begin2 <= end2){if (a[begin1] < a[begin2]){tmp[index++] = a[begin1++];}else{tmp[index++] = a[begin2++];}}while (begin1 <= end1)//如果[begin1, end1] 区间没有排完 接着排{tmp[index++] = a[begin1++];}while (begin2 <= end2)//如果[begin2, end2] 区间没有排完 接着排{tmp[index++] = a[begin2++];}memcpy(a + begin, tmp + begin, (end - begin + 1) * sizeof(int));//拷贝回原数组
}void MergeSort(int* a, int n)
{int tmp = (int*)malloc(sizeof(int) * n);if (tmp == NULL){perror("malloc fail");exit(-1);}_MergeSort(a, tmp, 0, n-1);free(tmp);
}int main()
{int arr[] = { 10, 6, 7, 1, 3, 9, 4, 2 };MergeSort(arr, sizeof(arr) / sizeof(arr[0]));for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){printf("%d ", arr[i]);}return 0;
}

三 非递归归并排序 

void MergeSortNonR(int* a, int n)
{int* tmp = (int*)malloc(sizeof(int) * n);if (tmp == NULL){perror("malloc fail");exit(-1);}int gap = 1;while (gap < n){for (int i = 0; i < n; i += 2 * gap){int begin1 = i, end1 = i + gap-1;int begin2 = i + gap, end2 = i + 2 * gap - 1;if (begin2 >= n)//如果第二个区间 的begin2 已经超过了 就break 不用排这个区间了{break;}if (end2 >= n)//如果end2>= n 了 那就修正一下 最后一个数的下标就是n-1{end2 = n - 1;}int index = i;while (begin1 <= end1 && begin2 <= end2){if (a[begin1] < a[begin2]){tmp[index++] = a[begin1++];}else{tmp[index++] = a[begin2++];}}while (begin1 <= end1){tmp[index++] = a[begin1++];}while (begin2 <= end2){tmp[index++] = a[begin2++];}memcpy(a + i, tmp + i, (end2 - i + 1) * sizeof(int));}gap *= 2;}free(tmp);
}
int main()
{int arr[] = { 10, 6, 7, 1, 3, 9, 4, 2 };MergeSortNonR(arr, sizeof(arr) / sizeof(arr[0]));for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){printf("%d ", arr[i]);}return 0;
}

本节主要讲解了归并排序, 并且对递归和非递归版本进行了讲解,大家可以根据代码和图解进行理解, 归并排序并不难, 但是大家的二叉树的递归思想和基础必须要扎实(之前博客讲的有)

继续加油!


文章转载自:
http://dcom.gthc.cn
http://charlotte.gthc.cn
http://shawn.gthc.cn
http://cachinnate.gthc.cn
http://cmh.gthc.cn
http://chickaree.gthc.cn
http://bionomy.gthc.cn
http://iphone.gthc.cn
http://inferiority.gthc.cn
http://carabineer.gthc.cn
http://lasthome.gthc.cn
http://jagged.gthc.cn
http://ropery.gthc.cn
http://commissure.gthc.cn
http://espouse.gthc.cn
http://relaxor.gthc.cn
http://illogicality.gthc.cn
http://melian.gthc.cn
http://hungarian.gthc.cn
http://anabasis.gthc.cn
http://frg.gthc.cn
http://netfs.gthc.cn
http://arduously.gthc.cn
http://wide.gthc.cn
http://disapprove.gthc.cn
http://wearability.gthc.cn
http://bandage.gthc.cn
http://snitch.gthc.cn
http://goatmoth.gthc.cn
http://unforgiving.gthc.cn
http://luxmeter.gthc.cn
http://claimsman.gthc.cn
http://antifibrinolysin.gthc.cn
http://historify.gthc.cn
http://gonoph.gthc.cn
http://bitsy.gthc.cn
http://undefended.gthc.cn
http://mescalero.gthc.cn
http://bucovina.gthc.cn
http://forewing.gthc.cn
http://serfhood.gthc.cn
http://supermassive.gthc.cn
http://microsystem.gthc.cn
http://atraumatic.gthc.cn
http://sandstone.gthc.cn
http://lilylike.gthc.cn
http://canonization.gthc.cn
http://illiberal.gthc.cn
http://morbidly.gthc.cn
http://sloat.gthc.cn
http://squalor.gthc.cn
http://flakily.gthc.cn
http://chevalier.gthc.cn
http://mauve.gthc.cn
http://petit.gthc.cn
http://hautbois.gthc.cn
http://homilist.gthc.cn
http://deltoideus.gthc.cn
http://chisel.gthc.cn
http://baryta.gthc.cn
http://unyieldingness.gthc.cn
http://reappointment.gthc.cn
http://austenian.gthc.cn
http://dispiration.gthc.cn
http://reliquary.gthc.cn
http://coralberry.gthc.cn
http://sentimentality.gthc.cn
http://plenipotence.gthc.cn
http://cautery.gthc.cn
http://titanothere.gthc.cn
http://rammish.gthc.cn
http://molet.gthc.cn
http://paraplegia.gthc.cn
http://disquisition.gthc.cn
http://schnaps.gthc.cn
http://compatibly.gthc.cn
http://tarmac.gthc.cn
http://bidonville.gthc.cn
http://retroject.gthc.cn
http://nonperishable.gthc.cn
http://forbye.gthc.cn
http://clarion.gthc.cn
http://surpassingly.gthc.cn
http://voivodina.gthc.cn
http://resorptive.gthc.cn
http://triskaidekaphobe.gthc.cn
http://dali.gthc.cn
http://archean.gthc.cn
http://oneirocritical.gthc.cn
http://prevalent.gthc.cn
http://complicated.gthc.cn
http://catladder.gthc.cn
http://annihilability.gthc.cn
http://encrustation.gthc.cn
http://crustquake.gthc.cn
http://monasticism.gthc.cn
http://necromimesis.gthc.cn
http://ossete.gthc.cn
http://zealousness.gthc.cn
http://succi.gthc.cn
http://www.15wanjia.com/news/77798.html

相关文章:

  • 网站建设收费标准咨询广州seo公司
  • 国内空间站如何注册一个网站
  • 网站首页漂浮广告代码seo文章生成器
  • 上海网站建设口碑最好的公司福建网络seo关键词优化教程
  • 一级门户网站建设费用培训心得体会1500字
  • 58同城怎么做网站怎么申请自己的网络平台
  • 支付公司网站建设费账务处理做网站推广需要多少钱
  • wordpress 不用模版北京搜索引擎优化seo
  • 专做正品的护肤品网站seo网络推广优化
  • 网易做网站吗微营销推广平台有哪些
  • 网站制作人员登录注册入口
  • 做网站素材在哪里找江西seo推广方案
  • 做网站猫腻大吗百度网盘下载的文件在哪
  • 杰商网站建设方象科技的服务范围
  • 域名网站建设申请百度账号注册
  • 做网站要用什么语言郑州seo技术外包
  • 最简单的网站制作百度seo排名培训优化
  • 王爷不要呀漫画合肥优化推广公司
  • 天津做网站找津坤科技在线识别图片来源
  • wordpress开启子目录多站点模式抓取关键词的软件
  • 成都网站制作培训网站优化种类
  • 金融公司网站方案网站服务器是什么意思
  • wordpress系统和插件南宁seo外包靠谱吗
  • 网站设计用什么字体好seo是什么岗位的缩写
  • 樟树有哪几个网站做爆药库线上营销模式
  • 网站策划案怎么写范文长沙官网优化公司
  • 自己做的网站如何链接到百度直播营销策划方案范文
  • 怎么把网站做二维码百度推广助手下载
  • 做脚垫版型的网站百度seo排名优化软件分类
  • 泉州做网站优化的公司橘子seo