4-1 Iterative Mergesort (9分)

6-1 Iterative Mergesort (25分) 6-1 Iterative Mergesort (25分) How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one sorted list is obtained. Y 阅读详情

How would you implement mergesort without using recursion?

The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one sorted list is obtained. You are supposed to implement the key function of merging.

Format of functions:

void merge_pass( ElementType list[], ElementType sorted[], int N, int length );

The function merge_pass performs one pass of the merge sort that merges adjacent pairs of sublists from list into sorted. N is the number of elements in the list and length is the length of the sublists.

Sample program of judge:

#include <stdio.h>

#define ElementType int
#define MAXN 100

void merge_pass( ElementType list[], ElementType sorted[], int N, int length );

void output( ElementType list[], int N )
{
    int i;
    for (i=0; i<N; i++) printf("%d ", list[i]);
    printf("\n");
}

void  merge_sort( ElementType list[],  int N )
{
    ElementType extra[MAXN];  /* the extra space required */
    int  length = 1;  /* current length of sublist being merged */
    while( length < N ) { 
        merge_pass( list, extra, N, length ); /* merge list into extra */
        output( extra, N );
        length *= 2;
        merge_pass( extra, list, N, length ); /* merge extra back to list */
        output( list, N );
        length *= 2;
    }
} 


int main()
{
    int N, i;
    ElementType A[MAXN];

    scanf("%d", &N);
    for (i=0; i<N; i++) scanf("%d", &A[i]);
    merge_sort(A, N);
    output(A, N);

    return 0;
}

/* Your function will be put here */

Sample Input:

10
8 7 9 2 3 5 1 6 4 0

Sample Output:

7 8 2 9 3 5 1 6 0 4 
2 7 8 9 1 3 5 6 0 4 
1 2 3 5 6 7 8 9 0 4 
0 1 2 3 4 5 6 7 8 9 

0 1 2 3 4 5 6 7 8 9

void merge_pass( ElementType list[], ElementType sorted[], int N, int length )
{
	int now=0;
	int top=0;
	int left, right;
	while(now<N)
	{
		left=now;
		right=now+length;
		while(left<now+length && right<now+2*length && left<N && right<N)
		{
			if(list[left]<list[right])
			{
				sorted[top++]=list[left++];
			}
			else
			{
				sorted[top++]=list[right++];
			}
		}
		while(left<now+length && left<N)
		{
			sorted[top++]=list[left++];
		}
		while(right<now+2*length && right<N)
		{
			sorted[top++]=list[right++];
		}
		now=now+2*length;
	}
}
贵校的PTA作业,请自行绕道,以免不必要的麻烦

从零开始:如何在STM32上搭建QP框架实现事件驱动开发(附完整代码) 本文详细介绍了如何在STM32嵌入式平台上从零开始搭建QP框架,实现事件驱动开发。通过对比传统前后台系统与RTOS的局限,深入剖析了QP框架基于活动对象和层次式状态机的核心优势,并提供了完整的环境配置、代码移植及一个智能灯光控制器的实战案例,帮助开发者构建清晰、可维护的复杂嵌入式应用。 阅读详情

相关推荐

【COMSOL】将一个面上数据导出CSV或txt格式的方法

在COMSOL中参数化扫描后,把一个面上或体里的数据按照CSV或TXT格式导出的方法。

dave496206的博客 2991

二、Merge sort

1 问题 2 算法 2.1 伪代码 2.2 算法思想 2.3 手工演示 2.4 Python实现 在这里插入代码片 3 算法分析

runing 275

CAPL实战:从零构建HEX文件解析器,赋能UDS刷写

本文详细介绍了如何使用CAPL从零构建HEX文件解析器,以支持UDS刷写操作。通过解析HEX文件格式、设计状态机、优化数据结构与算法,实现高效准确的固件数据处理,并提供了与UDS刷写流程对接的工程化实践建议。

weixin_28726391的博客 444

INT202-Complexity of Algorithms: Sorting Algorithms 排序算法

找到插入节点z(即新的最后一个节点)。·堆序性:对于除根节点以外的每个内部节点v,节点v的键值大于或等于其父节点的键值,即key(v) ≥ key(parent(v))。·父节点的键值不大于其子节点的键值。- 优先队列抽象数据类型的`removeMin`方法对应于从堆中移除根节点的键值。·第一阶段:通过一系列n次`insertItem`操作,将集合C中的元素放入一个初始为空的优先队列P中。- 当键值到达一个叶子节点,或者到达一个其子节点的键值都大于或等于的节点时,`downheap`操作结束。

hhsstt__的博客 1053

13-单片机模块化程序: 单片机AT指令配置模块程序模板(非阻塞版)

资料源码:https://gitee.com/yang456/OpenProgrammingModuleForMCU.git 点击加入群聊【单片机,物联网,上位机】: 说明1:知识从未如此性感。 烂程序员关心的是代码,好程序员关心的是数据结构和它们之间的关系! 说明2:学的是思想,而非程序!此代码思路适用于所有的单片机。 说明3:学会以后,下面的代码可能会跟你一辈子! 说明4:这一系列文章是为大幅度裁剪本人博客文章!使博客文章更有条理。便于推其它教程! 拷贝这两个文件到自己的工程 ..

杨奉武的博客 1281

详解卷积神经网络(CNN)

卷积神经网络(Convolutional Neural Network, CNN)是一种前馈神经网络,它的人工神经元可以响应一部分覆盖范围内的周围单元,对于大型图像处理有出色表现。

qq_25762497的博客 25万+

iterative-mergesort

iterative-mergesort

yaozhili_的博客 1291

超过4.2万个AOI(地理围栏),深圳市2025年2月15日更新的全类别AOI,矢量图层(全国22个超大特大城市AOI更新系列)

超过4.2万个AOI(地理围栏),深圳市2025年2月15日更新的全类别AOI,矢量图层(全国22个超大特大城市AOI更新系列)

1072

PTA 分治法

其中List/* 保存线性表中最后一个元素的位置 */ };L是用户传入的一个线性表,其中元素可以通过>、==、<进行比较,并且题目保证传入的数据是递增有序的。函数要查找X在Data中的位置,即数组下标(注意:元素从下标1开始存储)。找到则返回下标,否则返回一个特殊的失败标记NotFound。while(l<r)// 将数组a的整数集合划分成两个不相关子集,使得|n1-n2|最小,且|S1-S2|最大,返回|S1-S2|的结果。

m0_52681311的博客 1316

[原创]Linux 802.11n CSI tool安装教程(亲测可用)

Linux 802.11n CSI tool安装教程 前言 网上关于Intel5300 CSI tool的教程繁多,实际有效的可以说极少,各种疏漏错误。装的时候被坑无数次,终于调通。其实装这个工具并不太难,但是很多人把它弄得极其复杂,所以写这篇博客来帮助和我有一样困扰的人。我基本沿用的是官方文档旧版本的安装方案,针对其冗杂之处做了大量改进。 旧版安装教程: http://dhalperi....

u014645508的博客 4万+

2025年GESP3月认证C++五级真题解析

小 A 有 2n 件物品,小 B 和小 C 想从小 A 手上买走这些物品。对于第 i 件物品,小 B 会以 bi 的价格购买,而小 C 会以 ci 的价格购买。对于所有测试点,保证 1 <= T <= 100, 2 <= g < p <= 10^9, g, p 为整数, p 为质数。对于所有测试点,保证 1 <= n <= 10^5, 0 <= bi, ci <= 10^9, bi, ci 为整数。对于另外 50 % 的测试点,保证 n <= 100000, 0 <= bi, ci <= 10^9。

lan_in的博客 1036

ZJU PTA ds 6-1 Iterative Mergesort

How would you implement mergesort without using recursion?

s_w_s_的博客 943

AXI-5.6 Multiple region interfaces

规则:如果从设备实现的区域少于16个,它必须确保对任何访问未支持区域的尝试都能给出符合协议的响应。具体实现方式由设计者定义。场景:一个错误的软件驱动试图向区域 5(该加密加速器未实现)写入数据。实现方式一:提供错误响应(严格模式)行为:加密加速器检测到AxREGION的值大于2,立即对该事务返回一个DECERR(解码错误)或SLVERR(从设备错误)响应。举例:这类似于你访问一个不存在的网站,服务器返回“404 Not Found”。它明确告知请求者:“你要找的东西不存在”。优点安全、清晰。

weixin_44700185的博客 672

4-1 Iterative Mergesort   (9分)

4-1 Iterative Mergesort   (9分) How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a

叶鸿飞的博客 2309

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Iterative Mergesort

【代码】Iterative Mergesort。

Xm041206的博客 480

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

6-1 Iterative Mergesort

【代码】6-1 Iterative Mergesort。

2203_75646004的博客 541

6-15 Iterative Mergesort (25分)

How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one ...

weixin_43301333的博客 1084

c编写Iterative Mergesort

Iterative Mergesort   (9分) How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a

vason的博客 1326

浙大PTA4-1 Iterative Mergesort 的case

其实不是什么重要的,只是第二个case和第四个case一直通不过,但自己拿来验证的case几乎都是对的,所以琢磨了很久这两个case是什么。刚开始以为最后一个是针对left==right,因为没写这个if的时候这一点是运行超时。后来发现N为奇数的时候,在length=1即两两比较的时候就会出错(sorted里最后一个没有输入),针对这个情况进行修改后,非常意外的,最后一个case通过了,第二个还是

Sybil_aoi的博客 1423

6-2 Iterative Mergesort

How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one sorted list is obtained. You are supposed to implement

星空不见云的博客 925

6-15 Iterative Mergesort(25 分)

6-15 Iterative Mergesort(25 分)How would you implement mergesort without using recursion?The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of...

友人A 2802

6-2 Iterative Mergesort (25 分)

How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pair of adjacent sublists until one...

CodeMonkey 2870

6-15 Iterative Mergesort

6-15Iterative Mergesort(25 分) How would you implement mergesort without using recursion? The idea of iterative mergesort is to start from N sorted sublists of length 1, and each time to merge a pa...

lannister_awalys_pay的博客 892

数据结构基础hw12Iterative Mergesort函数题

归并过程中,只要有一个子序列先用完,另一个子序列剩下的元素本身已经有序,所以不用再比较,直接接到后面即可。的有序子序列合并到辅助数组中,若末尾不足两个完整子序列,则根据剩余元素数量决定继续合并或直接复制。其实它已经是一个有序段了,但没有另一个长度为 2 的段和它合并,所以直接复制。两个子序列都是有序的,所以每次只需要比较两个当前元素,把较小的放入。说明从第 8 个位置开始,已经不够两个完整长度为 2 的子序列了。第一个子序列长度是 8,第二个子序列长度只有 2。第三,处理数组末尾不够两个完整子序列的情况。

2301_80025611的博客 296

6-15 Iterative Mergesort (25 分)

void merge_pass( ElementType list[], ElementType sorted[], int N, int length ){ int h1=0,e1=length-1,h2=length,e2=2*length-1; //h1、h2分别是两个归并数组的开头,e1、e2是结尾 int k=0,last=0; while(1){ // printf("%d %d - %d %d\n",h1,e1,h2,e2); if(e2&

Jay98的博客 315

Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)

题目链接:点击打开链接 题目大意:略。 解题思路:略。 AC 代码 void merge_pass( ElementType list[], ElementType sorted[], int N, int length ) { int idx=0, p1=0, len1=p1+length, p2=len1, len2=p2+length; int f...

947

Mergesort

Mergesort:import java.util.*; public class MergerSort { public static void main(String[] args) { Integer[] a = {2, 6, 3, 5, 1}; mergeSort(a); System.out.println(Arrays.toString(a)); } publ

1400
上一篇: Project 2
下一篇: 5-1 Insertion or Heap Sort (9分)
csucbMIT
博客等级 码龄10年 7粉丝 15原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值