#include <stdio.h>
#define INFINITY 999999
#define LEN(A) ((sizeof (A)) / (sizeof A[0])) void print_array(int A[], int len)
{
int i;
/*
int len = LEN(A);
sizeof on array function parameter will return size of 'int *' instead of 'int []'
[-Wsizeof-array-argument]
int len = LEN(A);
^
*/
for (i = 0; i < len; i++)
printf("%d, ", A[i]);
putchar('\n');
}
//merge将本次合并的两个array分别打印
void merge(int A[], int p, int q, int r)
{
int k, i, j;
int n1 = q - p + 1;
int n2 = r - q;
int L[n1+1], R[n2+1]; for (i = 0; i < n1; i++)
L[i] = A[p+i];
L[n1] = INFINITY;
print_array(L, n1+1); for (j = 0; j < n2; j++)
R[j] = A[q+1+j];
R[n2] = INFINITY;
print_array(R, n2+1); i = j = 0;
for (k = p; k <= r; k++) {
if (L[i] <= R[j]) {
A[k] = L[i];
i++;
} else {
A[k] = R[j];
j++;
}
}
} void mergeSort(int A[], int p, int r)
{
if (p < r) {
int q = (p+r)/2;
mergeSort(A, p, q);
mergeSort(A, q+1, r);
merge(A, p, q, r);
}
} int main(int argc, char *argv[])
{
int A[] = {5, 6, 7, 8, 9, 10, 11, 12, 12, -3, 1, 2, 3, 4};
merge(A, 0, 8, 13);
print_array(A, LEN(A)); int B[] = {2, 4, 5, 7, 1, 3, 2, 6};
mergeSort(B, 0, LEN(B)-1);
print_array(B, LEN(B)); return 0;
}

分治-1-归并排序(Divide and Conquer-1-merge sort)的更多相关文章

  1. The Divide and Conquer Approach - 归并排序

    The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...

  2. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  3. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  4. 【高级排序算法】1、归并排序法 - Merge Sort

    归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...

  5. 归并排序(Merge Sort)

    归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...

  6. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  7. js 实现排序算法 -- 归并排序(Merge Sort)

    原文: 十大经典排序算法(动图演示) 归并排序 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得 ...

  8. 【算法】归并排序(Merge Sort)(五)

    归并排序(Merge Sort) 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...

  9. 归并排序(merge sort)

    M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...

  10. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

随机推荐

  1. Postgresql实现不同用跨模式访问

    1.修改参数 2.创建两个用户 创建a1 create user a1 connection limit-1 password '123456'; alter user a1 SUPERUSER; g ...

  2. string str = string.Empty也会出错?

    如题 为什么会出现这种情况?大佬解释一下.

  3. navicat无法识别登录秘钥

    前因 公司数据库未开放外网访问端口,只允许内网登录,这对开发人员查看数据很不友好,所以一般情况下都会让开发人员通过navicat的ssh隧道功能来查看数据. 但在测试ssh隧道过程中,私钥1验证通过, ...

  4. vue中如何在子组件添加类似于watch属性监听父组件数据,数据变化时子组件做出相应的动作

    首先:我们需要在父组件中标签中定义一个 ref="parentObjVue" 其次:我们在子组件中,通过  var tmp=this.$refs.parentObjVue找到父组件 ...

  5. xml简单操作

    1.创建简单的XML 1 XmlDocument XmlDoc = new XmlDocument(); 2 //XML声明 3 var xmlDeclaration = XmlDoc.CreateX ...

  6. virtualenvwrapper使用命令

    virtualenvwrapper使用命令 创建虚拟环境:mkvirtualenv + test1 查看虚拟环境:lsvirtuslenv 删除虚拟环境:rmvirtualenv + test1 退出 ...

  7. 如何使用visual studio code的插件remote ssh远程操作virtual box虚拟机

    0 Remote-SSH是什么?为什么要用它? The Remote-SSH extension lets you use any remote machine with a SSH server a ...

  8. Minio--docker部署

    拉取镜像 docker pull minio/minio 启动容器 创建文件夹 bin data config 启动脚本 docker run -p 9000:9000 -p 9001:9001 \ ...

  9. RabbitMQ的使用介绍

    一.RabbitMQ是什么 RabbitMQ是一种常用的消息中间件,是基于AMQP协议,采用erlang语言开发的面向消息服务的中间件,是一个独立的系统应用程序,可以管理服务器计算资源和网络通信.一般 ...

  10. PHP_冒泡排序代码解析

    <?php /** * 基本思想:将数组中的每一个下标元素遍历出来 *依次将这些下标的值对后面一个下标的值对比 *如果大于后面一位下标的值,将两者调换位置 */ $arr = array (55 ...