-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsort.cpp
More file actions
59 lines (43 loc) · 1.1 KB
/
sort.cpp
File metadata and controls
59 lines (43 loc) · 1.1 KB
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
#include <iostream>
#include <cstdlib>//for "exit()" on some systems
#include <vector>
using namespace std;
/**
\fn SortAnalysis
\brief Sort data
\param [in] The "array" A[0..n-1] containing items to be sorted
\returns The total number of key comparisons made
*/
int SortAnalysis(auto& A )
{
int count = 0u;
for (int i = 1u; i < A.size(); i++)
{
int v = A[i];
int j = i - 1;
while (j >= 0 and A[j] > v )
{
count = count + 1;
A[j + 1] = A[j];
j = j -1;
}
A[j + 1] = v;
}
return count;
}
int main()
{
vector<int> inputs;
int input;
cerr<<"Welcome to \"SortAnalysis\". We first need some input data."<<endl;
cerr<<"To end input type Ctrl+D (followed by Enter)"<<endl<<endl;
while(cin>>input)//read an unknown number of inputs from keyboard
{
inputs.push_back(input);
}
cerr<<endl<<"| Number of inputs | Number of comparisons |"<<endl;
cerr<<"|\t"<<inputs.size();
cout<<"| "<<SortAnalysis(inputs);
cerr<<"\t|"<<endl<<endl<<"Program finished."<<endl<<endl;
return 0;
}