C++计算运行时间和随机数的插入排序
2017-03-15 17:08
232 查看
#include<iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
void InsertionSort (int a[],int len)
{
if (a==NULL||len<=1)
{
return ;
}
for(int j=1;j<len;j++)
{
int temp=a[j];
int p=j;
while(p>0&&a[p-1]>temp)
{
a[p]=a[p-1];
p--;
}
a[p]=temp;
}
}
void InsertionTwoSort (int a[],int len)
{
if (a==NULL||len<=1)
{
return ;
}
for(int j=1;j<len;j++)
{
int temp=a[j];
//可以考虑二分插入
int start=0,end=j;
while(start<end)
{
int mid=start+(end-start)/2;
if (a[mid]<temp)
{
start=mid+1;
}
else
end=mid;
}
int p=j;
while(p>end)
{
a[p]=a[p-1];
p--;
}
a[p]=temp;
}
}
void SortRunTime(int a[],int length)
{
clock_t start,end;
start=clock();
InsertionTwoSort(a,length);
end=clock();
cout<<" Insert RunTime : "<<(double)(end-start)/CLOCKS_PER_SEC;
}
int rand(int p,int q)
{
int size=q-p+1;
return p+rand()%size;
}
int main()
{
int a[100000];
int num=100000;
for (int i=0;i<num;i++)
{
a[i]=rand(0,200000);
}
SortRunTime(a,num);
return 0;
}
#include<cstdlib>
#include <ctime>
using namespace std;
void InsertionSort (int a[],int len)
{
if (a==NULL||len<=1)
{
return ;
}
for(int j=1;j<len;j++)
{
int temp=a[j];
int p=j;
while(p>0&&a[p-1]>temp)
{
a[p]=a[p-1];
p--;
}
a[p]=temp;
}
}
void InsertionTwoSort (int a[],int len)
{
if (a==NULL||len<=1)
{
return ;
}
for(int j=1;j<len;j++)
{
int temp=a[j];
//可以考虑二分插入
int start=0,end=j;
while(start<end)
{
int mid=start+(end-start)/2;
if (a[mid]<temp)
{
start=mid+1;
}
else
end=mid;
}
int p=j;
while(p>end)
{
a[p]=a[p-1];
p--;
}
a[p]=temp;
}
}
void SortRunTime(int a[],int length)
{
clock_t start,end;
start=clock();
InsertionTwoSort(a,length);
end=clock();
cout<<" Insert RunTime : "<<(double)(end-start)/CLOCKS_PER_SEC;
}
int rand(int p,int q)
{
int size=q-p+1;
return p+rand()%size;
}
int main()
{
int a[100000];
int num=100000;
for (int i=0;i<num;i++)
{
a[i]=rand(0,200000);
}
SortRunTime(a,num);
return 0;
}
相关文章推荐
- c++ 生成随机数、计算运行时间
- c++中计算运行时间 clock() clock_t 示例:比较递归和非递归数值运算
- 『转』C++中计算程序的运行时间
- [转]c++中计算运行时间 clock(),clock_t程序
- C++中计算某个代码的运行时间
- c++中计算运行时间 clock() clock_t 示例:比较递归和非递归数值运算
- C++程序运行时间的计算
- C++高精度实现计算程序运行时间
- 计算程序的运行时间(C++)
- C/C++中算法运行时间的三种计算方式
- C++高精度计算代码运行时间
- C++实现计算程序运行时间 高精度
- c++中计算运行时间 clock(),clock_t程序
- C++程序运行时间计算
- C++中计算程序的运行时间
- C++ 计算代码运行时间的几种方法(转)
- C++中计算程序的运行时间
- 一个c++中计算算法运行时间的程序
- c++计算程序运行时间的函数,怎么才能精确到微妙级呢?
- C++中程序运行时间的计算