-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_sort.cpp
111 lines (91 loc) · 3.15 KB
/
insert_sort.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <chrono>
using namespace std;
void print(vector<int> &in); // print whole vector. Using for testing
void insert_sort(vector<int> &in);
int main(int argc, char const *argv[])
{
// init variable------------------------------------------------------------------------------
ifstream fin;
ofstream fout;
int size, buffer;
vector<int> data;
// clock_t start, end;
// open files ---------------------------------------------------------------------------------
fin.open("input");
if (!fin) {
cout << "file in failed" << endl;
return 0;
}
fout.open("output");
if (!fout) {
cout << "file in failed" << endl;
return 0;
}
// get data from input file --------------------------------------------------------------------
fin >> size;
cout << "Size: " << size << endl;
for(int i = 0; i < size; i++) {
fin >> buffer;
if (buffer == EOF) {
cout << "file size error: " << buffer ;
return 1;
}
data.push_back(buffer);
}
// pirnt raw data--------------------------------------------------------------------------------
// cout << "raw: ";
// print(data);
// count start time -----------------------------------------------------------------------
// start = clock();
auto begin = chrono::high_resolution_clock::now();
// cout << start << endl;
// sorting
insert_sort(data);
// print sorted data --------------------------------------------------------------------------
// cout << "sort: ";
// print(data);
// count end time ----------------------------------------------------------------------------------
// end = clock();
auto terminal = chrono::high_resolution_clock::now();
// cout << end << endl;
// print running time --------------------------------------------------------------------
// cout << "Sorting time: " << (double)(end - start) << " ms" << endl;
auto dur = terminal - begin;
auto running_time = std::chrono::duration_cast<std::chrono::nanoseconds>(dur).count();
cout << "Running time: " << running_time << " nanoseconds" << endl;
// write sorted data to output file -----------------------------------------------------------
fout << data.size() << endl;
for(int i = 0; i < (int)data.size(); i++) {
fout << data[i] << endl;
}
// end program stuff----------------------------------------------------------------------------
fin.close();
fout.close();
return 0;
}
void print(vector<int> &in)
{
for(int i = 0; i < (int)in.size(); i++) {
cout << in[i] << " ";
}
cout << endl;
}
void insert_sort(vector<int> &in)
{
for(int i = 1; i < (int)in.size(); i++) {
int key = in[i];
// insert target into queue
int j = i - 1;
while(i >= 0 && in[j] > key) {
in[j+1] = in[j];
j--;
}
in[j+1] = key;
}
}