插入排序
#include <iostream>
#include <ctime>
#include <iomanip>
#include <Windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int val[] = {4,2,7,1,8,9,6};
int Key;
int i ;
LARGE_INTEGER m_liPerfFreq={0};
QueryPerformanceFrequency(&m_liPerfFreq); //获取CPU的时钟频率
LARGE_INTEGER m_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart); //获取时钟初始值
for (int j = 1; j < 7; j++)
{
Key = val[j];
i = j - 1;
while(i >= 0 && val[i] > Key)
{
val[i+1] = val[i];
val[i] = Key;
i = i - 1;
}
}
LARGE_INTEGER liPerfNow={0};
QueryPerformanceCounter(&liPerfNow); //获取结束时时钟值
double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart; //两次时钟计数差
time /=m_liPerfFreq.QuadPart; //除于时钟频率,单位为秒(s)
time *= 1000.0; //转换成毫秒(ms)
i = 0;
while(i < 7)
{
cout << val[i] << " ";
i++;
}
cout << endl;
cout<<"插入排序时间消耗:"<< time <<" ms";
cout << endl;
return 0;
}
执行效果并输出这个算法执行所花的时间
算法导论里的伪代码介绍
对数组 A = {5 , 2 , 4 , 6 , 1 , 3} 排序图解
作者“bizhu的专栏”
补充:软件开发 , C语言 ,