C语言实现快速排序并对其进行计时

news/2024/5/20 0:36:00 标签: 快速排序, c语言
#include <stdio.h>
#include <time.h>
#include <stdlib.h> 
#include <windows.h> 
#define MAXSIZE 100000				//生产随机数个数

void Quick_sort(int a[],int left,int right)		//快速排序
{
	int i=left,j=right;
	int center = a[(i+j)/2];
	int tran;
	
	while(i <= j)
	{
		while(a[i] < center)
		{
			i++;
		}
		while(a[j] > center)
		{
			j--;
		}
		if(i<=j)
		{
			tran = a[i];
			a[i] = a[j];
			a[j] = tran;
			i++;j--;
		}
	}
	if(left < j)
	{
		Quick_sort(a,left,j);
	}
	if(i < right)
	{
		Quick_sort(a,i,right);
	}
 } 
 
 void RWriteData(char *FileName, int size, int type)		//写出随机生成数
{
	int i,j;
	FILE *fp;
	srand((unsigned)time(NULL));
	if ((fp=fopen(FileName, "w"))==NULL)
	{
		printf("No such file exist.");
		exit (0);
	}
	if (type==1)
	{
		printf("\n------------------random integers----------------\n");
	}
	for (i=0;i<size;i++)
	{
		j=rand()%99999;
		fprintf(fp,"%d   ", j);
		if (type==1)
		{ 
			printf("%d   ",j);
		}
	}
	printf("\n"); 
	fclose(fp);
}

void GetData(char *FileName,int a[])
{
	FILE *fp;
	if ((fp=fopen(FileName, "r"))==NULL)
	{
		printf("No such file exist.");
		exit (0);
	}
	else
	{
		int data,i=0;
		while(fscanf(fp,"%d",&data)!= EOF )
		{
			a[i]=data;
			i++;
		 } 
	}
	fclose(fp);
}
 
 void WriteData(char *FileName,int a[])		//存储数据
 {
	FILE *fp;
	if((fp = fopen(FileName,"w"))==NULL)
	{
		printf("No such file exist.");
		exit (0);
	 }
	 else
	 {
	 	for(int i=0; i<MAXSIZE; i++)
	 	{
	 		fprintf(fp,"%d ",a[i]);
		 }
	 }
	 fclose(fp);
 }
 
 void StroeTime(char *FileName,double a,int i)	//存储时间
 {
 	FILE *fp;
 	if(i==1)
 	{
 		if((fp = fopen(FileName,"w"))==NULL)
		{
			printf("No such file exist.");
			exit (0);
		 }
	 }
	else
	{
		if((fp = fopen(FileName,"a"))==NULL)
		{
			printf("No such file exist.");
			exit (0);
		 }
	}
 	fprintf(fp,"%f\n",a);
	fclose(fp);
 }
 
 int main()
 {
	int array[MAXSIZE];
	double len_time;
	_LARGE_INTEGER starttime,endtime;
	double Freq;			//计时器频率 
	_LARGE_INTEGER f; 		//计时器频率
	QueryPerformanceFrequency(&f);
	Freq=(double)f.QuadPart;
	
	
	RWriteData("a.txt",MAXSIZE,1);
	GetData("a.txt",array);
	
	QueryPerformanceCounter(&starttime); 
	Quick_sort(array,0,MAXSIZE-1);
	QueryPerformanceCounter(&endtime); 
	len_time=(endtime.QuadPart-starttime.QuadPart)/Freq;
	StroeTime("c.txt",len_time,1);
	printf("此次快速排序耗时%fs\n",len_time);
	
	printf("------------------------快速排序后-----------------------------\n");
	
	WriteData("b.txt",array);
	GetData("b.txt",array);
	QueryPerformanceCounter(&starttime);
	Quick_sort(array,0,MAXSIZE-1);
	QueryPerformanceCounter(&endtime);
	len_time=(endtime.QuadPart-starttime.QuadPart)/Freq;
	StroeTime("c.txt",len_time,2);
	printf("排好序后再次快速排序耗时%fs\n",len_time);
	
	
 }
 

运行结果

在这里插入图片描述
快速排序
在这里插入图片描述
快速排序
在这里插入图片描述

存储时间(单位,秒)
在这里插入图片描述


http://www.niftyadmin.cn/n/1731831.html

相关文章

C语言实现二叉树ADT、二叉排序树和二叉树遍历

#include <stdio.h> #include <stdlib.h> #include <time.h> #include "btree.h" #define COUNT 20void CreateBTree(BTNode *&b,char *str) //接收一个符号表示法的二叉树字符串&#xff0c;创建为一个二叉树结构 {BTNode *St[MAXSIZE],*p…

C语言实现冒泡排序和折半查找

#include <stdio.h> #include <time.h> #include <stdlib.h> #define MAXSIZE 8000typedef struct //创建顺序表&#xff0c;count用于记录查找次数 {int data[MAXSIZE];int count0; }SqList;void Bubble_Sort(SqList *&L,int i) //实现冒泡排…

环信创建群组html,群组管理

群组管理获取群组列表调用listRooms函数获取当前登录用户加入的群组列表&#xff0c;示例如下。// 列出所有群组var listGroups function () {var option {success: function (rooms) {console.log(rooms);},error: function () {console.log(List groups error);}};conn.lis…

计算机应用程序错误怎么办 6,电脑开机无图标 显示svchost.exe应用程序错误该怎么办?...

有一段时间没有用电脑了&#xff0c;发现一开机会出现这个界面&#xff0c;确定后桌面一片空白且只有壁纸。现在来说明这个问题的解决方案1、出现这个问题一般等几分钟后&#xff0c;会出现桌面图标的。系统程序加载的慢造成的原因&#xff0c;这样的小问题不用重装系统的&…

华科计算机学院考研专业课,华中科技大学计算机学院软件与理论考研经验

华中科技大学计算机学院软件与理论考研经验2010届考生&#xff0c;跨专业、跨届、跨学校、跨省、辞职&#xff0c;工作两年后辞职复习&#xff0c;总计复习时间三个月整&#xff0c;考入华中科技大学作者flytiger3768次阅读2011-05-132010届考生&#xff0c;跨专业、跨届、跨学…

你的计算机无法启动怎么回事,电脑无法正常启动如何做系统-“你的电脑未能正确启动”的解决方法...

电脑未能启动的情况下如何给电脑重装系统&#xff1f;首先你得准备一个(U盘启动盘)&#xff0c;不会如何利用U盘做启动盘的请百度系统之家&#xff0c;上面有教程&#xff0c;或者直接上某宝几十块买一个。等你有U盘启动盘后&#xff0c;插电脑&#xff0c;开机时狂按Delete键进…

计算机信息管理基础...,2007-2008第一学期计算机信息管理基础课程试卷A

2007-2008第一学期计算机信息管理基础课程考试试卷A一、单项选择题(1分/每小题&#xff0c;共20分)1.信息的属性包括()。A 事实性、变换性、传递性B 时效性、依附性、价值性C 等级性、压缩性、可视性D 共享性、事实性、规律性2.在信息管理的发展历程中&#xff0c;最早的时期是…

华南理工计算机电路基础试题,华南理工2016计算机电路基础作业,求答案

满意答案lm01372016.05.27采纳率&#xff1a;51% 等级&#xff1a;9已帮助&#xff1a;613人1&#xff0c;解&#xff1a;Vb Va 7VVd Us 10VUbc Vb - Vc 5VUad Va - Vd -3VUda Vd - Va 3V电压等于电位差&#xff0c;电位等于该点对参考点的电压值。2&#xff0c;解…