⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 internalcompare.cpp

📁 这个程序包括了各种常用内部排序算法在平均排序所需时间上的比较.
💻 CPP
字号:
// InternalCompare.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream.h>
#include "CInterCmp.h"
void RandomAndSave();//产生随机数并保存;
void CompareEachSort();
void menu();

void begin()                                  //界面函数;
{
   
	cout<<endl<<"            ★★★★★★★★★★★★★★★★★★★★★★★★★"<<endl;
	cout<<"          ★★★★★★★★★★★★★★★★★★★★★★★★★★★"<<endl;
    cout<<"        ★★★                                              ★★★"<<endl;
	cout<<"       ★★★     欢迎使用小丁子软件之各种内部排序法比较     ★★★"<<endl;
	cout<<"        ★★★★                                          ★★★★"<<endl;
	cout<<"          ★★★★★★★★★★★★★★★★★★★★★★★★★★★ "<<endl;
    cout<<"            ★★★★★★★★★★★★★★★★★★★★★★★★★"<<endl;


}
int main(int argc, char* argv[])
{
    char ch1[5],anotherchoice;
	int  choice;
	begin();

    menu();
	cin>>ch1;
	choice=atoi(ch1);
	while(choice!=0)
	{
		if((choice==12)||(choice>12)||(choice<=0))
		   exit(0);
		cout<<"随机数尚未产生,不能进行内部排序的比较!"<<endl;
		cout<<"请先产生随机数!"<<endl;
		menu();
		cin>>ch1;
	    choice=atoi(ch1);
	}
flag:cout<<"随机数已产生,现在你可以进行内部排序的比较了!"<<endl;
	RandomAndSave();
flag1:	menu();
	cin>>ch1;
	choice=atoi(ch1);
    switch(choice)
	{
	case 0:cout<<"随机数已产生!"<<endl;
		     cout<<"你想产生另外一组随机数吗?  y/n:";
			 cin>>anotherchoice;
			 if(anotherchoice=='y')
				 goto flag;
			 else
				 goto flag1;
	case 1:CMergeSort mergesort;//归并排序
             mergesort.DealMergeSort();
			 goto flag1;
	case 2:CBinSort binsort;//折半插入排序
             binsort.Deal();
			 goto flag1;
	case 3:CShellSort shellsort;//希尔排序
		     shellsort.DealShellSort();
			 goto flag1;
	case 4:CBubbleSort bubblesort;//起泡排序
             bubblesort.Deal();
	         goto flag1;
	case 5:CSimpSecSort simpsecsort;//简单选择排序
             simpsecsort.Deal();
			 goto flag1;
	case 6:CQuickSort quicksort;//快速排序
             quicksort.Deal();
			 goto flag1;
	case 7:CHeapSort heapsort;//堆排序
             heapsort.Deal();
			 goto flag1;
	case 8:CDirSort dirsort;//直接插入排序
             dirsort.Deal();
			 goto flag1;
	case 9:CRadixSort radixsort;//基数排序
		     radixsort.DealRadix();
			 goto flag1;
	case 10:CTwoWayInsertSort twowayinsertsort;
		    twowayinsertsort.Deal();
		    goto flag1;
	case 11:CompareEachSort();
		     goto flag1;
	case 12:cout<<"                                  感谢使用本软件!"<<endl;
		    break;
	}
    return 0;
}
////////////////////////////////////////
//产生随机数并保存
void RandomAndSave()
{
	int i;  
    time_t t;  
    srand((unsigned) time(&t)); 
	FILE *fp;
	if((fp=fopen("test.txt","w"))==NULL)
	{
		cout<<"文件不能打开!"<<endl;
		exit(0);
	}
	fprintf(fp,"此文件的记录个数是:100\n");
	for(i=1;i<=100;i++)
	    fprintf(fp,"%d\n",rand()%100+100);
	fclose(fp);
}
/////////////////////////////////////////////////////////////////////////////////////////
void CompareEachSort()
{
	CMergeSort mergesort;//归并排序
    mergesort.DealMergeSort1();
	CBinSort binsort;//折半插入排序
    binsort.Deal1();
	CShellSort shellsort;//希尔排序
    shellsort.DealShellSort1();
	CBubbleSort bubblesort;//起泡排序
    bubblesort.Deal1();
	CSimpSecSort simpsecsort;//简单选择排序
    simpsecsort.Deal1();
	CQuickSort quicksort;//快速排序
    quicksort.Deal1();
	CHeapSort heapsort;//堆排序
    heapsort.Deal1();
	CDirSort dirsort;//直接插入排序
    dirsort.Deal1();
	CRadixSort radixsort;//基数排序
    radixsort.DealRadix1();
	CTwoWayInsertSort twowayinsersort;//2-路插入排序
	twowayinsersort.Deal1();
	cout<<endl;
	cout<<endl;
	cout<<endl;
	cout<<"                ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
	cout<<"                ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
	int i=0;//起始序号
	cout<<"\t\t序号\t排序名称\t比较次数\t移动次数"<<endl;
	cout<<"\t\t"<<++i<<"\t归并排序\t"<<mergesort.Cmp_Time<<"\t\t"<<mergesort.Move_Time<<endl;
	cout<<"\t\t"<<++i<<"\t折半插入排序\t"<<binsort.cmp_time<<"\t\t"<<binsort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t希尔排序\t"<<shellsort.cmp_time<<"\t\t"<<shellsort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t起泡排序\t"<<bubblesort.cmp_time<<"\t\t"<<bubblesort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t简单选择排序\t"<<simpsecsort.cmp_time<<"\t\t"<<simpsecsort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t快速排序\t"<<quicksort.cmp_time<<"\t\t"<<quicksort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t堆排序\t\t"<<heapsort.cmp_time<<"\t\t"<<heapsort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t直接插入排序\t"<<dirsort.cmp_time<<"\t\t"<<dirsort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t基数排序\t"<<radixsort.cmp_time<<"\t\t"<<radixsort.move_time<<endl;
	cout<<"\t\t"<<++i<<"\t2-路插入排序\t"<<twowayinsersort.cmp_time<<"\t\t"<<twowayinsersort.move_time<<endl;
}

/////////////////////////////////////////////////////////////////////////////////////////

void menu()//主   菜   单
{
		cout<<endl;
	    cout<<endl;
		cout<<"                           内部排序算法比较系统                     "<<endl;
		cout<<"              ▃▃▃▃▃▃▃▃▃▃▃主  菜  单▃▃▃▃▃▃▃▃▃▃    "<<endl;
        cout<<"              ▎0.产生随机数                                      ▎    "<<endl;
		cout<<"              ▎┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  ▎  "<<endl;
    	cout<<"              ▎1.归并排序        2.折半插入排序      3.希尔排序  ▎    "<<endl;
	    cout<<"              ▎4.起泡排序        5.简单选择排序      6.快速排序  ▎    "<<endl;  
		cout<<"              ▎7.堆排序          8.直接插入排序      9.基数排序  ▎    "<<endl;
		cout<<"              ▎10.二路插入排序                                   ▎    "<<endl;
		cout<<"              ▎┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅  ▎    "<<endl;
  	    cout<<"              ▎11.各种排序的总比较                  12.退出      ▎    "<<endl;  
 		cout<<"              ▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃    "<<endl;
		cout<<"                                                                        "<<endl;
	    cout<<"                                  请你选择:";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -