#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的專欄”