-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSorts.cpp
71 lines (50 loc) · 1.69 KB
/
InsertionSorts.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Insertion sorts
//
// Compiler: Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)
#include <iostream>
#include <chrono>
#include <random>
#include <vector>
const unsigned int NUM_ELEMENTS = 1000;
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds milliseconds;
std::vector<unsigned int> generateRandomVector(int size)
{
std::default_random_engine generator((unsigned int)time(0));
std::uniform_int_distribution<unsigned int> distribution(0, UINT_MAX);
std::vector<unsigned int> vectorToSort;
vectorToSort.reserve(size); // Pre-allocate memory but do not initialize variables.
for (int x = 0; x < size; x++)
vectorToSort.push_back(distribution(generator));
return vectorToSort;
}
void insertionSort (std::vector<unsigned int>& vectorToSort)
{
int size = vectorToSort.size();
int current, inspection, temp;
for (current = 1; current < size; current++)
{
inspection = current;
temp = vectorToSort[current];
while (inspection > 0 && temp < vectorToSort[inspection - 1])
{
vectorToSort[inspection]=vectorToSort[inspection - 1];
--inspection;
}
vectorToSort[inspection] = temp;
}
}
int main()
{
std::cout << "Starting - Insertion sort." << std::endl;
auto vectorToSort = generateRandomVector(NUM_ELEMENTS);
Clock::time_point t0 = Clock::now();
insertionSort(vectorToSort);
Clock::time_point t1 = Clock::now();
milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
auto throughput = NUM_ELEMENTS / ms.count();
std::cout << std::endl << "Throughput was: " << throughput << " Sorts / ms." << std::endl;
std::cout << "Complete - Press ENTER to exit." << std::endl;
std::cin.get();
return 0;
}