算法实验2.2、2.3

news2024/10/7 11:22:14

2.2主要内容

比较快速排序,归并排序以及堆排序算法的时间效率。了解影响算法执行时间的 主要因素以及如何降低算法的执行时间。 

#include<iostream>
using namespace std;
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<time.h>
#include <sys/timeb.h>
#include"string.h"
#pragma warning(disable : 4996)

typedef struct RedType {
	int key;
}RedType;
typedef struct SqList {
	RedType* r;
	int Length;
}SqList;

SqList* CreateRandomSqList(int sqListLen) {
	SqList* sq;
	int i;
	sq = (SqList*)malloc(sizeof(SqList));
	//输入序列长度
	sq->Length = sqListLen;
	sq->r = (RedType*)malloc(sizeof(RedType) * (sq->Length + 1)); //给序列的每个元素分配空间
	srand((unsigned)(time(NULL)));
	for (i = 1; i <= sq->Length; i++) {
		sq->r[i].key = int(rand());
	}
	return sq;  //返回序列起始地址
}
//创建一个与csp一样的存储空间
SqList* CopyRandomSqList(SqList* csp) {
	SqList* sq;
	int i;
	sq = (SqList*)malloc(sizeof(SqList));
	//输入序列长度
	sq->Length = csp->Length;
	sq->r = (RedType*)malloc(sizeof(RedType) * (sq->Length + 1)); //给序列的每个元素分配空间
	for (i = 1; i <= sq->Length; i++) {
		sq->r[i].key = csp->r[i].key;
	}
	return sq;  //返回序列起始地址
}
void WritetoFile(int num, int sortTime[], FILE* fp) {
	char ch[20];
	for (int i = 0; i < num; i++) {
		_itoa_s(sortTime[i], ch, 10);
		strcat_s(ch, ",");
		fwrite(ch, sizeof(char), strlen(ch), fp);
	}
	fwrite("\n", sizeof(char), 1, fp);
}

//快速排序
void QuickSort(RedType q[], int l, int r) {
	if (l >= r)return;
	int i = l - 1, j = r + 1, x = q[l + r >> 1].key;
	while (i < j) {
		do i++; while (q[i].key < x);
		do j--; while (q[j].key > x);
		if (i < j)swap(q[i].key, q[j].key);
	}
	QuickSort(q, l, j);
	QuickSort(q, j + 1, r);
}
//堆排序
void HeapAdjust(RedType* SR, int s, int m)//一次筛选的过程
{
	int rc, j;
	rc = SR[s].key;
	for (j = 2 * s; j <= m; j = j * 2)//通过循环沿较大的孩子结点向下筛选
	{
		if (j < m&& SR[j].key < SR[j + 1].key) j++;//j为较大的记录的下标
		if (rc > SR[j].key) break;
		SR[s] = SR[j]; s = j;
	}
	SR[s].key = rc;//插入
}
void HeapSort(RedType* SR, int n)
{
	int temp, i, j;
	for (i = n / 2; i > 0; i--)//通过循环初始化顶堆
	{
		HeapAdjust(SR, i, n);
	}
	for (i = n; i > 0; i--)
	{
		temp = SR[1].key;
		SR[1].key = SR[i].key;
		SR[i].key = temp;//将堆顶记录与未排序的最后一个记录交换
		HeapAdjust(SR, 1, i - 1);//重新调整为顶堆
	}
}
//归并排序
void aMerge(RedType* SR, int i, int m, int n) {
	int j, k;
	for (j = m + 1; j <= n; j++) {
		if (SR[j].key <= SR[j - 1].key) {
			int temp = SR[i].key;
			for (k = j - 1; temp < SR[k].key && k >= i; --k)
				SR[k + 1].key = SR[k].key;
			SR[k + 1].key = temp;
		}
	}
	return;
}
void aMSort(RedType* SR, int s, int t)
{
	if (s < t) {
		int mid = (s + t) / 2;
		aMSort(SR, s, mid);
		aMSort(SR, mid + 1, t);
		aMerge(SR, s, mid, t);
	}
	return;
}
void MergeSort(SqList* L) {
	aMSort(L->r, 1, L->Length);
	return;
}

int main() {

	SqList* L, * L1, * L2;
	struct __timeb64 stime, etime;
	long int rmtime, rstime;
	char ch[20];
	int quickSortTime[105], mergeSortTime[105], heapSortTime[105];
	FILE* fp;
	fp = fopen("Curv.csv", "w");

	for (int i = 10000; i <= 1000000; i = i + 10000) {
		_itoa_s(i, ch, 10);
		strcat_s(ch, ",");
		fwrite(ch, sizeof(char), strlen(ch), fp);
	}
	fwrite("\n", sizeof(char), 1, fp);
	int k = 0;
	for (int i = 10000; i <= 1000000; i = i + 10000) {
		L = CreateRandomSqList(i);
		L1 = CopyRandomSqList(L);
		L2 = CopyRandomSqList(L);
		//归并排序
		_ftime64_s(&stime);
		MergeSort(L);
		_ftime64_s(&etime);
		free(L->r);
		free(L);
		rstime = etime.time - stime.time;
		rmtime = rstime * 1000;
		rmtime += etime.millitm - stime.millitm;
		mergeSortTime[k] = rmtime;

		//快速排序
		_ftime64_s(&stime);
		QuickSort(L1->r, 0, L1->Length - 1);
		_ftime64_s(&etime);
		free(L1->r);
		free(L1);
		rstime = etime.time - stime.time;
		rmtime = rstime * 1000;
		rmtime += etime.millitm - stime.millitm;
		quickSortTime[k] = rmtime;

		//堆排序
		_ftime64_s(&stime);
		HeapSort(L2->r, L2->Length - 1);
		_ftime64_s(&etime);
		free(L2->r);
		free(L2);
		rstime = etime.time - stime.time;
		rmtime = rstime * 1000;
		rmtime += etime.millitm - stime.millitm;
		heapSortTime[k] = rmtime;
		k++;
	}
	WritetoFile(100, mergeSortTime, fp);
	WritetoFile(100, quickSortTime, fp);
	WritetoFile(100, heapSortTime, fp);
	fclose(fp);
	return 1;
}

2.3主要内容 

 学习分析递归程序结构的时间复杂度和影响算法运行时间的因素。

#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<time.h>
#include <sys/timeb.h>
#include"string.h"
#pragma warning(disable : 4996)
#define M 4
#define N 15
using namespace std;

class Color {
	friend int mColoring(int, int, int**);
public:
	bool Ok(int k);
	void Backtrack(int k);
	int n, m, ** a, * x;
	long sum;
};
long long  cifang(int n,int m)
{
	int count = 0;long long c = 1;
	for (int i = 0; i < n; i++)
	{

		c = c*(m/2);//防止时间复杂度太大了,进行缩小
		count++;
	}
	return c;
}
void WritetoFile(int num, int sortTime[], FILE* fp) {
	char ch[20];
	for (int i = 0; i < num; i++) {
		_itoa_s(sortTime[i], ch, 10);
		strcat_s(ch, ",");
		fwrite(ch, sizeof(char), strlen(ch), fp);
	}
	fwrite("\n", sizeof(char), 1, fp);
}
int mColoring(int, int, int**);
int main() {
	int n, m, ** a, sum; long long timecom{ 0 };
	//cout << "Please input the number of colors:";
	//cin >> m;

	struct __timeb64 stime, etime;
	long int rmtime, rstime;
	char ch[20];
	int paintTime[105]{0};
	FILE* fp;
	fp = fopen("Curv.csv", "w");

	for (int i = 5; i <= N; i = i + 1) {
		_itoa_s(i, ch, 10);
		strcat_s(ch, ",");
		fwrite(ch, sizeof(char), strlen(ch), fp);
	}
	fwrite("\n", sizeof(char), 1, fp);

	int k{ 0 };
	/*for (int i = 5; i <= N; i = i + 1) {

		//cout << "Please input the number of nodes:";
		n=i;
		a = new int* [n + 1];
		for (int i = 0; i <= n; i++) {
			a[i] = new int[n + 1];
		}
		//cout << "Please input the ralation of nodes:1-->Connected,0-->Not connected";
		for (int i = 1; i <= n; i++) {
			for (int j = i + 1; j <= n; j++) {
				//cout << "please input the ralation of" << i << "and" << j << ":";
				//cin >> a[i][j];
				//a[i][j] = 1;//完全图,最外面的i循环换成 i <= M,注意要改四处
				a[i][j] = 0;//一条边都没有,最外面的i循环换成 i <= N
				a[j][i] = a[i][j];
			}
		}
		_ftime64_s(&stime);

		sum = mColoring(n, M, a);

		_ftime64_s(&etime);

		rstime = etime.time - stime.time;
		rmtime = rstime * 1000;
		rmtime += etime.millitm - stime.millitm;
		paintTime[k] = rmtime;
		k++;

		delete[]a;
	}

	WritetoFile(11, paintTime, fp);

	for (int i = 5; i <= N; i = i + 1)
	{
		timecom = i * cifang(i,M);

		_itoa_s(timecom, ch, 10);
		strcat_s(ch, ",");
		fwrite(ch, sizeof(char), strlen(ch), fp);
	}
	fwrite("\n", sizeof(char), 1, fp);*/

	k = 0;
	n = 10;
	for (int j = 5; j <= N; j = j + 1) {

		//cout << "Please input the number of nodes:";
		m = j;
		a = new int* [n + 1];
		for (int i = 0; i <= n; i++) {
			a[i] = new int[n + 1];
		}
		//cout << "Please input the ralation of nodes:1-->Connected,0-->Not connected";
		for (int i = 1; i <= n; i++) {
			for (int j = i + 1; j <= n; j++) {
				//cout << "please input the ralation of" << i << "and" << j << ":";
				//cin >> a[i][j];
				//a[i][j] = 1;//完全图,注意要改四处
				a[i][j] = 0;//一条边都没有
				a[j][i] = a[i][j];
			}
		}
		_ftime64_s(&stime);

		sum = mColoring(n, m, a);

		_ftime64_s(&etime);

		rstime = etime.time - stime.time;
		rmtime = rstime * 1000;
		rmtime += etime.millitm - stime.millitm;
		paintTime[k] = rmtime;
		k++;

		delete[]a;
	}

	WritetoFile(11, paintTime, fp);

	for (int i = 5; i <= N; i = i + 1)
	{
		timecom = n * cifang(n,i);

		_itoa_s(timecom, ch, 10);
		strcat_s(ch, ",");
		fwrite(ch, sizeof(char), strlen(ch), fp);
	}
	fwrite("\n", sizeof(char), 1, fp);

	fclose(fp);

	//cout << sum << endl;
	return 1;
}
bool Color::Ok(int k)
{
	for (int j = 1; j <= n; j++)
		if ((a[k][j] == 1 && x[j] == x[k]))
			return false;
	return true;
}
void Color::Backtrack(int t) {
	if (t > n) {
		sum++;
		/*for (int i = 1; i <= n; i++)
			cout << x[i] << " ";
		cout << endl;*/
	}
	else {
		for (int i = 1; i <= m; i++) {
			x[t] = i;
			if (Ok(t))Backtrack(t + 1);
			x[t] = 0;
		}
	}
}
int mColoring(int n, int m, int** a) {
	Color X;
	X.n = n;
	X.m = m;
	X.a = a;
	X.sum = 0;
	int* p = new int[n + 1];
	for (int i = 0; i <= n; i++)p[i] = 0;
	X.x = p;
	X.Backtrack(1);
	delete[]p;
	return X.sum;
}

时间复杂度分析:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1883350.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Gradle学习-4 创建二进制插件工程

二进制插件工程创建有两种方式&#xff1a; 创建独立的工程&#xff0c;调试的时候&#xff0c;需要手动发布成一个二进制插件jar包&#xff0c;给其他工程里面引用&#xff0c;进行功能测试。这种方式是比较麻烦的。创建buildSrc子工程&#xff0c;它是一个大工程中的子工程&…

C语言之线程的学习

线程属于某一个进程 共同点&#xff1a;都能并发 线程共享变量&#xff0c;进程不共享。 多线程任务中&#xff0c;其中某一个线程调用了exit了&#xff0c;其他线程会跟着一起退出 如果是特定的线程就调用pthread_exit 失败返回的是错误号 下面也是

基于JSP技术的校园餐厅管理系统

开头语&#xff1a; 你好呀&#xff0c;我是计算机学长猫哥&#xff01;如果您对校园餐厅管理系统感兴趣或有相关需求&#xff0c;欢迎随时联系我。我的联系方式在文末&#xff0c;期待与您交流&#xff01; 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#x…

Redis 中的通用命令(命令的返回值、复杂度、注意事项及操作演示)

Redis 中的通用命令(高频率操作) 文章目录 Redis 中的通用命令(高频率操作)Redis 的数据类型redis-cli 命令Keys 命令Exists 命令Expire 命令Ttl 命令Type命令 Redis 的数据类型 Redis 支持多种数据类型&#xff0c;整体来说&#xff0c;Redis 是一个键值对结构的&#xff0c;…

深入解析 androidx.databinding.Bindable 注解

在现代 Android 开发中&#xff0c;数据绑定 (Data Binding) 是一个非常重要的技术。它使得我们能够简化 UI 和业务逻辑之间的连接&#xff0c;从而提高代码的可读性和维护性。在数据绑定中&#xff0c;Bindable 注解是一个关键部分&#xff0c;它帮助我们实现双向数据绑定和自…

什么是脏读、幻读、不可重复读

数据库事务 数据库事务是指作为单个逻辑工作单元执行的一系列操作&#xff0c;这些操作要么全部成功执行&#xff0c;要么全部失败回滚&#xff0c;以保持数据库的一致性和完整性。在多线程或多用户同时操作时&#xff0c;难免会出现错乱与冲突&#xff0c;这就需要引入事务的…

零成本、高效率:免费可视化工具的魅力所在

在如今这个数据驱动的时代&#xff0c;免费可视化工具越来越受到人们的欢迎。这些工具不仅降低了数据分析的门槛&#xff0c;还为用户提供了强大的功能和极高的灵活性&#xff0c;使得各行各业的人们都能够轻松地利用数据做出明智的决策。首先&#xff0c;免费可视化工具的零成…

SpringBoot学习06-[SpringBoot与AOP、SpringBoot自定义starter]

SpringBoot自定义starter SpringBoot与AOP SpringBoot与AOP 使用AOP实现用户接口访问日志功能 添加AOP场景启动器 <!--添加AOP场景启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</…

使用Pogo-DroneCAN CANHUB扩展板扩展飞控的CAN口

关键词&#xff1a;Ardupilot&#xff0c;Pixhawk&#xff0c;DroneCAN CANHUB扩展&#xff0c;扩展飞控CAN口 keywords&#xff1a;Ardupilot&#xff0c;Pixhawk&#xff0c;DroneCAN CANHUB Extend 摘要&#xff1a;使用Pogo-DroneCAN CANHUB扩展板扩展飞控CAN口&#xff…

数据挖掘常见算法(分类算法)

K&#xff0d;近邻算法&#xff08;KNN&#xff09; K-近邻分类法的基本思想&#xff1a;通过计算每个训练数据到待分类元组Zu的距离&#xff0c;取和待分类元组距离最近的K个训练数据&#xff0c;K个数据中哪个类别的训练数据占多数&#xff0c;则待分类元组Zu就属于哪个类别…

五分钟了解MQ消息集成

一、MQ消息集成的定义 MQ消息集成是通过消息中间件&#xff08;Message Queue&#xff09;实现的一种数据集成方式。它通过将数据发送到中间件中&#xff0c;再从中间件中接收数据&#xff0c;实现不同系统之间的数据交换。在MQ消息集成中&#xff0c;发送者和接收者之间不需要…

(六)Shader

Shader Shader(着色器)&#xff1a;一种运行在GPU端的类C语言GLSL&#xff0c;用于处理顶点数据以及决定像素片元最终着色。 Shader对三角形数据的处理&#xff0c;分为顶点处理和片元处理&#xff0c;分别称为顶点着色器(Vertex Shader)和片元着色器(Fragment Shader) GLSL …

Ansible 最佳实践:现代 IT 运维的利器

Ansible 最佳实践&#xff1a;现代 IT 运维的利器 Ansible 是一种开源的 IT 自动化工具&#xff0c;通过 SSH 协议实现远程节点和管理节点之间的通信&#xff0c;适用于配置管理、应用程序部署、任务自动化等多个场景。本文将介绍 Ansible 的基本架构、主要功能以及最佳实践&a…

最简单的Qt连接MYSQL的方法

1.报错原因 Qt在某个版本后不在提供编译好的mysql驱动&#xff0c;只在src中提供了驱动源码&#xff0c;使用需要自行编译链接&#xff0c;报错信息如下&#xff1a; QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL Q…

BUT000增强字段BAPI结构激活出错(BUPA_CENTRAL_CI_CHANGE)

导语&#xff1a;BP主数据增强字段&#xff0c;需要使用BAPI&#xff1a;BUPA_CENTRAL_CI_CHANGE进行值写入&#xff0c;但是在SAP 2023以后的版本&#xff0c;激活会出错&#xff0c;原因是因为SAP的一个结构同时包含了BUS00_EEW以及BUS00_EEWX两个结构&#xff0c;导致结构字…

A股低开高走,近3000点,行情要启动了吗?

A股低开高走&#xff0c;近3000点&#xff0c;行情要启动了吗&#xff1f; 今天的A股&#xff0c;让人瞪目结舌了&#xff0c;你们知道是为什么吗&#xff1f;盘面上出现2个重要信号&#xff0c;一起来看看&#xff1a; 1、今天两市低开高走&#xff0c;银行板块护盘指数&…

教你如何在群晖上部署m3u8视频下载工具,支持浏览器一键添加下载任务

文章目录 📖 介绍 📖🏡 演示环境 🏡📒 文章内容 📒📝 快速开始📝 群晖部署📝 部署浏览器一键添加任务🎈 常见问题 🎈⚓️ 相关链接 ⚓️📖 介绍 📖 在当今数字化时代,视频内容的下载和管理变得越来越重要。尤其是对于那些使用群晖NAS设备的用户,一…

Android Studio 解决AAPT: error: file failed to compile

1.找到项目下的build.gradle 2.在android语块中添加下面代码 aaptOptions.cruncherEnabled false aaptOptions.useNewCruncher false 12

scratch宇航员太空漫游 2024年6月中国电子学会图形化编程 少儿编程 scratch编程等级考试一级真题和答案解析

目录 scratch宇航员太空漫游 一、题目要求 1、准备工作 2、功能实现 二、案例分析 1、角色分析 2、背景分析 3、前期准备 三、解题思路 1、思路分析 2、详细过程 四、程序编写 五、考点分析 六、 推荐资料 1、入门基础 2、蓝桥杯比赛 3、考级资料 4、视频课程…

如何使用AI学习一门编程语言?

无论你是软件开发新手还是拥有几十年的丰富经验&#xff0c;总是需要学习新知识。TIOBE Index追踪50种最受欢迎的编程语言&#xff0c;许多生态系统为职业发展和横向转型提供了机会。鉴于现有技术具有的广度&#xff0c;抽空学习一项新技能并有效运用技能可能困难重重。 最近我…