-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
41 lines (40 loc) · 929 Bytes
/
main.cpp
File metadata and controls
41 lines (40 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include "clock.h"
using namespace std;
void insertion_sort(vector<int> & v)
{
int insert, moveItem;
for(int next=1;next<v.size();++next)
{
insert = v.at(next);
moveItem = next;
while((moveItem>0) &&
(v.at(moveItem-1) > insert))
{
v.at(moveItem) = v.at(moveItem-1);
--moveItem;
}
v.at(moveItem) = insert;
}
}
int main()
{
Clock clk;
const int size = 1000000;
vector<int> v1(size),v2;
srandom(time(NULL));
for(int i=0; i<size; ++i)
v1.at(i) = random();
v2 = v1; clk.start();
sort(v1.begin(), v1.end());
cout << "sort(): " << clk.getElapsedTime() << " seconds\n";
cout << "v1/v2 are "<<((v1==v2)?"the same.\n":"different.\n");
clk.start();
insertion_sort(v2);
cout << "insertion_sort(): " <<clk.getElapsedTime() << " seconds\n";
cout << "v1/v2 are "<<((v1==v2)?"the same.\n":"different.\n");
return 0;
}