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

📄 test.cpp

📁 这算法实现了插入排序
💻 CPP
字号:
#include<stdlib.h>
#include<time.h>
#include<fstream.h> 
#include"comp.h"
#include"inssort.h"
#include"shell.h"
#include"qsort.h"
#include"heapsort.h"
#include"swap.h"


   clock_t tstart = 0;  // 开始运行时间初始值
   double T_run=0;        // 设置程序运行时间

  void Settime()     // Initialize the program timer
  { tstart = clock(); }

double Gettime()                       // Return the elapsed time since the last call to Settime
  { return (double)((double)clock() - (double)tstart)/
  (double)CLOCKS_PER_SEC; } 

int main()
{
   ifstream input_file;   //输入文件流
   ofstream output_file;  //输出文件流
   int A[1024],B[10240];
   int loop;//循环变量
cout<<"**********************插入排序****************************"<<endl;
//对RandIn1K进行操作
  output_file.open("RandIn1K.Dat");    //打开文件RandIn1K作写操作
  if(!output_file){
   cout<<"Can not open file!"<<endl;
   return 1;
  }
  srand(1);
  for(loop=0;loop<1024;loop=loop+1)
	 {
     A[loop]=rand()%(1024);
	 output_file<<A[loop]<<" "; //将数据输入到文件中
	  }
    output_file.close();
  
  //  以只读方式打开文件
 input_file.open("RandIn1K.Dat");
     if(!input_file){
        cout<<"Can not open the file!"<<endl;
        return 1;
	 }
      for(loop=0;loop<1024;loop=loop+1)
	  {
       input_file>>A[loop];  
	  }
	  input_file.close();
     Settime();
   inssort<int,Compare>(A,1024);     //调用插入排序
  T_run=Gettime();               //获得当前程序运行时间
 
 output_file.open("IsOut1K.dat");    //打开文件RandIn100K作写操作
  if(!output_file){
   cout<<"Can not open file!"<<endl;
   return 1;
  }
  for(loop=0;loop<1024;loop=loop+1)
	 {
	 output_file<<A[loop]<<" "; //将数据输入到文件中
	  }
    output_file.close();
  
  //  以只读方式打开文件
 input_file.open("IsOut1K.dat");
     if(!input_file){
        cout<<"Can not open the file!"<<endl;
        return 1;
	 }
   input_file.close();
   	 cout<<endl;
  cout<<"********该RandIn1K.Dat总共运行"<<T_run<<"*************"<<endl;

   //对RandIn100K进行操作
  output_file.open("RandIn100K.Dat");    //打开文件RandIn1K作写操作
  if(!output_file){
   cout<<"Can not open file!"<<endl;
   return 1;
  }
  srand(1);
  for(loop=0;loop<10240;loop=loop+1)
	 {
     B[loop]=rand()%(10240);
	 output_file<<B[loop]<<" "; //将数据输入到文件中
	  }
    output_file.close();
  
  input_file.open("RandIn100K.Dat");
     if(!input_file){
        cout<<"Can not open the file!"<<endl;
        return 1;
	 }
      for(loop=0;loop<10240;loop=loop+1)
	  {
       input_file>>B[loop];  
	  }
	  input_file.close();
     Settime();
   inssort<int,Compare>(B,10240);     //调用插入排序
   T_run=Gettime();               //获得当前程序运行时间
 
 output_file.open("IsOut100K.dat");    //打开文件RandIn100K作写操作
  if(!output_file){
   cout<<"Can not open file!"<<endl;
   return 1;
  }
  for(loop=0;loop<10240;loop=loop+1)
	 {
	 output_file<<B[loop]<<" "; //将数据输入到文件中
	  }
    output_file.close();
  
  input_file.open("IsOut100K.dat");
     if(!input_file){
        cout<<"Can not open the file!"<<endl;
        return 1;
	 }
   input_file.close();
   	 cout<<endl;
  cout<<"********该RandIn100K.Dat总共运行时间"<<T_run<<"************"<<endl;
  return 0;

}

⌨️ 快捷键说明

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