分治-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 ...
随机推荐
- C++实现二叉树的定义与操作
头文件及常量定义 #include<stdio.h> #include<stdlib.h> #include<stdarg.h> #include<iostr ...
- pat 乙级 1025 反转链表
目前无法ac过最后一个测试点,其余ac,原因是有无效节点存在,但这个代码加上去除无效节点函数时便会运行超时 还没想出来怎么做,先这样吧 #include <stdio.h> #includ ...
- 如何设置VBA代码的密码?如何取消VBA代码的密码?
经常有网友问,用Access把软件开发好了,怎么样设置VBA代码的密码?以保护自己的代码不被同事修改.这里简单整理了一下.设置VBA代码的密码及取消VBA代码的密码步骤如下:1.打开任意一个窗体,进入 ...
- FTP调优
最近在解决客户的问题时接触到了一些FTP的问题,自己在使用过程中发现了很多问题,所以这里总结了一些调优的办法: 服务:vsftp 非常安全文件传输 配置文件:/etc/vsftpd/vsftpd.co ...
- 访问不通github的解决办法
访问不通github, 在hosts文件中手动加下域名IP 在hosts里添加github的ip 140.82.113.3 github.com #不要.199.232.5.194 github.gl ...
- Ubuntu上安装TensorFlow
一.更新环境 sudo apt-get update sudo apt-get install golang python3-dev python-dev libcupti-dev libjpeg- ...
- Windows 11在使用AMD时,CPU占用率持续100%的解决方案
一.现象 Windows 11在使用AMD时,CPU占用率持续100%,持续好几个系统版本都是如此 二.系统版本: 版本 Windows 11 专业工作站版版本 22H2安装日期 2022/10/ ...
- CodeGym自学笔记08——交互对象
交互对象 用 Java 语言编写的每个程序都由类和对象组成. 1."Java 程序员就像设计工程师一样,只是他们不制作蓝图,而是编写类.船舶零件是根据蓝图制造的,而对象是基于类创建的.&qu ...
- pychars的使用
1|0安装 pyecharts 兼容 Python2 和 Python3.目前版本为 0.1.2 pip install pyecharts 2|0入门 首先开始来绘制你的第一个图表 from pye ...
- angular 路由守卫Observable异步请求串联
假设路由守卫有这种场景 需要使用observable同时发送多个Http 请求,判断request2返回的数据中是否存在request1返回的数据 使用async await export class ...