分治-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 ...
随机推荐
- 苹果iPhone 日历查询功能异常
2022年底苹果发布了IOS16.2版本系统,此时间段内所有升级的用户都将会出现日历查询功能失效,字符错乱等诸多问题. 与客服沟通后告知,日历记录内容查询是没有时间限制的,可以无限期查询全部内容,于是 ...
- REST风格开发
使用测试工具 测试工具中 总结 @RequestBody:接收请求体参数Json @RequestParam:接收路径参数,包括表单 @PathVariable: 接收路径变量的. Rest风格优化 ...
- JS的有关递归的知识点(数据无限级联的实现)
所用测试数据: 1 const data = [ 2 { 3 "area_id": 5, 4 "name": "广东省", 5 " ...
- 关于xtr的一些基础
Q&A 如何找到QSEQ方式的xtt呢? 我们可以去版本的代码中寻找,具体位置在:(modem_proc/rf/rftarget_denali/mtp/xtt/etc/QSEQ/) 在QSEQ ...
- Win10 修改系统自带字体
文章来源: Win10怎样更改系统字体?Win10默认字体修改教程 - 系统之家 (xitongzhijia.net) 苹方字体下载,window系统专用[2021最新版]免费下载 - 知乎 (zhi ...
- 如何将PDF文件中的部分信息隐藏或遮盖呢?
由于工作需要,总是需要对PDF文件中的内容进行部分隐藏.之前,作为VIP,可以使用某软件对PDF中的信息进行部分遮盖,现在,VIP到期了,我也不想继续花钱了(哭穷,嘻嘻) 在信息时代,只要会百 ...
- vim自动输入P的问题
最近新租的centos8云服务器默认用的vim8.0,发现一个bug,就是每次打开文件,自动输入了一个P,原来是8.0的一个bug,升级版本可以解决 参考https://www.jianshu.com ...
- netmiko批量操作华为设备
from concurrent.futures import ThreadPoolExecutorimport timeimport netmikoimport osfrom threading im ...
- Flink RocksDB参数调优说明
参数名 说明 state.backend.rocksdb.block.blocksize block 的大小,默认值为4KB.在生产环境中总是会适当调大一些,一般32KB比较合适,对于机械硬盘可以再增 ...
- node 版本管理器 nvs
node 总是在不断的升级,以前老项目在运行时可能会报错 我遇到了一个 PostCSS received undefined instead of CSS string 查了下可能是node-sass ...