Мне нужно написать оценку и обоснование сложности получившегося алгоритма в O-нотации. Объясните, пожалуйста, как это сделать.
Код:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <chrono>
using namespace std;
vector<string> getSourceText();
vector<string> getWords(const vector<string>& lines);
void bubbleSort(vector<string>& words);
unsigned long long sortWordsByLength(vector<string>& words);
void writeResult(vector<string>& words);
void analysis(string task, const vector<string>& lines, const vector<string>& words, unsigned long long elapseTime);
void analysisImpl(ostream &out, string task, const vector<string>& lines, const vector<string>& words, unsigned long long elapseTime);
int main()
{
string task = "Variant 7. Latin, sort by length, descending order, allow number, bubble sort";
vector<string> lines = getSourceText();
vector<string> words = getWords(lines);
auto elapsed = sortWordsByLength(words);
writeResult(words);
analysis(task, lines, words, elapsed);
return 0;
}
vector<string> getSourceText()
{
cout << "Input path to file: ";
string pathToFile;
getline(cin, pathToFile);
ifstream in(pathToFile);
if (!in.is_open()) {
return vector<string>();
}
string line;
vector<string> lines;
while (getline(in, line)) {
lines.push_back(line);
}
in.close();
return lines;
}
vector<string> getWords(const vector<string> &lines)
{
string validSymbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string line;
vector<string> words;
for (auto line : lines) {
istringstream iss(line);
string word;
while (iss >> word) {
size_t pos = 0;
while ((pos = word.find_first_not_of(validSymbols, pos)) != string::npos) {
word.erase(pos, 1);
}
if (!word.empty())
words.push_back(word);
}
}
return words;
}
void writeResult(vector<string>& words) {
ofstream out("result.txt");
for (auto word : words)
out << word << endl;
out.close();
}
void bubbleSort(vector<string>& words)
{
for (int i = 0; i < words.size(); i++) {
for (int j = 0; j < words.size() - 1; j++) {
if (words[j].length() < words[j + 1].length()) {
swap(words[j], words[j + 1]);
}
}
}
}
unsigned long long sortWordsByLength(vector<string>& words)
{
auto start = std::chrono::steady_clock::now();
bubbleSort(words);
auto end = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
void analysis(string task, const vector<string>& lines, const vector<string>& words, unsigned long long elapsedTimeMs)
{
ofstream out("analysis.txt");
analysisImpl(out, task, lines, words, elapsedTimeMs);
out.close();
analysisImpl(cout, task, lines, words, elapsedTimeMs);
}
void analysisImpl(ostream &out, string task, const vector<string>& lines, const vector<string>& words, unsigned long long elapsedTimeMs)
{
out << "Source text:" << endl;
for (auto line : lines)
out << line << endl;
out << endl;
out << task << endl;
out << "Count words : " << words.size() << endl;
out << "Sort time : " << elapsedTimeMs << " ms" << endl << endl;
out << "Statistic. Count words specify length:" << endl;
map<size_t, size_t> appears;
for (int i = 0; i < words.size(); ++i) {
++appears[words[i].length()];
}
for (auto pair : appears)
out << "Length word " << pair.first << " appears " << pair.second << endl;
}