分治-1-归并排序(Divide and Conquer-1-merge sort)
#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)的更多相关文章
- The Divide and Conquer Approach - 归并排序
The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...
- 【高级排序算法】1、归并排序法 - Merge Sort
归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...
- 归并排序(Merge Sort)
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- js 实现排序算法 -- 归并排序(Merge Sort)
原文: 十大经典排序算法(动图演示) 归并排序 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得 ...
- 【算法】归并排序(Merge Sort)(五)
归并排序(Merge Sort) 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
随机推荐
- AOP中的一些重要术语简介
AOP的定义:AOP(Aspect Oriented Progamming)利用称为"横切"的技术,剖解开封装的对象内部,把多个类的公共行为封装到一个可重用模块中,便于减少重复代码 ...
- max virtual memory areas vm.max_map_count 65530 is too low的解决办法
解决办法 /etc/sysctl.conf加上 vm.max_map_count = 262144 使配置永久生效 执行: sysctl -w vm.max_map_count=262144 使配置立 ...
- npm报错最好的办法就是删掉依赖然后重装
之前有个node工程,现在要新增antd主题,那得增加安装 craco 并修改 package.json 里的 scripts 属性 改吧改,安装一直报错: Cannot find module 'w ...
- Linux(CentOS) Mysql 8.0.30 安装(多源安装)
Linux(CentOS) Mysql 8.0.30 安装(多源安装) 安装命令根据实际部署情况修改调整,CentOS一般选择通用版本Red Hat Enterprise Linux 7 本文档使用w ...
- huawei--配置链路聚合
huawei--配置链路聚合 项目要求: LSW1和LSW2之间配置链路聚合,链路聚合分为手工和lacp两种模式.配置完后查看链路聚合状态. 项目实施: (vlan10 20 30的创建命令vlan ...
- vim学习小结
参考书籍<Linux 从入门到精通>第二版(刘忆智 等编著) Vim编辑器 背景:Vim的设计哲学就是让使用者能够在主键盘区完成所有工作. vim是vi的增强版本,vim分为插入和命令两种 ...
- Qt头文件引用其他类,主类头文件报错(1)invalid use of incomplete type 'class xx::yy' (2)forward declaration of 'class xx::yy'
其实这个错误很蠢,由于代码是从cpp文件直接copy过来的就没仔细看,但是他这个报错很有迷惑性,我们来看图: 就这行代码,从cpp文件中复制过来的: 本来目的呢就是提升这个变量的作用域,但是呢!!!在 ...
- Win10系统将bat文件注册成服务
代码语法: sc create ServiceName binPath= 路径 start= auto 示例语句: sc create Tomcat binPath= F:/tomcat/bin/st ...
- shell脚本:报错syntax error near unexpected token `$'\r''解决方法
之前的shell脚本是在服务器上编写的,后来又已复制的方式存在在了电脑上,以txt文件的形式存放的.于是复制到了编辑工具中,进行了相应项的修改.修改完毕后,拿到服务器上测试,结果执行sh XXX.sh ...
- C# 海康威视网络半球摄像头回调YV12取画面
海康网络摄像头回调取画面,网口最好用千兆的网卡来做,开始用笔记本的百兆网口,不管怎么优化都是卡顿的, 后来用千兆网卡台式机的,基本就没有卡顿了,取图再加上运动检测处理,基本上十几毫秒每帧. 用回调方式 ...