Fortran 和 c++ 的 插入排序 insertion sort 代码

7 篇文章 0 订阅

插入排序法,Fortran代码如下:

subroutine insertionsort(a)
   implicit none 
   integer :: a(:)
   integer :: temp
   integer :: n , ii,jj
   n=size(a)

   do ii=2,n
      do jj=ii,2,-1
         if(a(jj) < a(jj-1)) then
            temp = a(jj)
            a(jj) = a(jj-1)
            a(jj-1) = temp 
         end if 
      end do 
   end do 

end subroutine insertionsort 

program main 

   implicit none 

   interface 
      subroutine insertionsort(a)
         integer :: a(:) 
      end subroutine 
   end interface 

   integer,allocatable :: a(:)
   integer :: number 

   write(*,*) "input number of numbers:"
   read(*,*) number 
   allocate(a(number))

   write(*,*) "input numbers: "
   read(*,*) a 

   call insertionsort(a)
   write(*,*) a 

end program 

尝试给100万个0-99的整数排序
每运行1000个记一次时间
代码如下:

subroutine insertionsort(a)
   implicit none 
   integer :: a(:)
   integer :: temp
   integer :: n , ii,jj
   real    :: t1,t2 
   n=size(a)

!    write(*,*) a
   open(unit=15,file="time.txt")
   do ii=2,n

      if (mod(ii,1000)==0) then 
         call cpu_time(t2)
         write(*,*) ii , "time: ",t2-t1
         write(15,*) ii/1000,t2-t1
         t1=t2
      end if 

      do jj=ii,2,-1
         if(a(jj) < a(jj-1)) then
            temp = a(jj)
            a(jj) = a(jj-1)
            a(jj-1) = temp 
         end if 
      end do 
   end do 

end subroutine insertionsort 

program main 

   implicit none 

   interface 
      subroutine insertionsort(a)
         integer :: a(:) 
      end subroutine 
   end interface 

   integer,allocatable :: a(:)
   integer  :: number ,ii
   real     :: t2,t1

!    write(*,*) "input number of numbers:"
!    read(*,*) number 
   number=1000000
   allocate(a(number))

!    write(*,*) "input numbers: "
!    read(*,*) a 
   open(12,file="r.txt")
   open(unit=13,file="out.txt")
   read(12,*) a 
   call cpu_time(t1)

   call insertionsort(a)
   call cpu_time(t2)
   write(*,*) t2-t1
   do ii=1,number 
    write(13,*) a(ii)
   enddo 

end program 

每1000次的循环记一次时间,画图
在这里插入图片描述
可以看出插入排序法的时间复杂度为O(N^2)

c++代码

#include <fstream>
#include <iostream>
using namespace std;

int main ()
{
   int a[1000000];
   int i;
   int ii , jj ,kk ;
   int temp;
   float t1,t2;

   ifstream infile;
   infile.open("r.txt");

   ofstream outfile;
   outfile.open("out.txt");

   for(i=0;i<999999;i++){
      infile >> a[i];      
      // if(i%100==0){
      //    cout << i << endl;
      // }
   }
   infile >> a[i];
   cout << a[2]<< ' '<<a[999999]<< endl;

   for(ii=2;ii<=999999;ii++){

      if(ii%1000==0){
         t2=clock()/1000000.;
         cout << ii << ' ' << t2-t1 << endl;
         outfile << ii << ' ' << t2-t1 << endl;

         t1=t2;
      };

      for(jj=ii;jj>=2;jj--){

         if(a[jj]<a[jj-1]){
            temp=a[jj];
            a[jj]=a[jj-1];
            a[jj-1]=temp;
         };
      };
   };
   return 0;
}

在这里插入图片描述
似乎c++比Fortran快一点

生成1000000个0-99的随机数的代码:

program main 

   real x 

   integer ii 

   open(unit=12,file='r.txt')

   do ii =1, 1000000
      call random_number(x)
      write(12,*) int(x*100)
   end do 
   close(12)

end 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值