Почему время одинаковое, хотя должно возрастать (сложность O(n^2) )

Petr

Member
Здравствуйте. Почему время одинаковое, хотя должно возрастать (сложность O(n^2) ) и как исправить?

Код:
#include <iostream>
#include <vector>
#include <random>
#include <ctime>
#include <algorithm>
#include <chrono>
#include <cstdlib>

using namespace std::chrono;

double meanTime(double* t, int n) {
double s{0};
for (int j{1}; j < 10; j += 1) s += t[j];
double aTime = s / 10;
std::cout << "\t [" << aTime << "] \t";
return aTime;
}
//Измерение std для пунктов a, b и c
double stdTime(double* t, double mean) {
double s{0};
for (int j{1}; j < 10; j += 1) s += (t[j] - mean) * (t[j] - mean);
double sTime = sqrt(s / 10);
std::cout << "[" << sTime << "]";
return sTime;
}

// a. Функция, сортирующая целые числа (int), которые размещены в векторе.
void SelectionSort(std::vector<int> &a) {
for (int startIndex = 0; startIndex < a.size() - 1; ++startIndex)
{
int smallestIndex = a.at(startIndex);
for (int currentIndex = startIndex + 1; currentIndex < a.size(); ++currentIndex)
{
if(a.at(currentIndex) < a.at(smallestIndex)) smallestIndex = currentIndex;

}
std::swap(a.at(startIndex), a.at(smallestIndex));
}
}

int RandomNumber2() { return (std::rand() % 100); }
void FillVector(std::vector<int>& a) {
std::srand(unsigned(std::time(0)));
std::generate(a.begin(), a.end(), RandomNumber2);
}

double measureTime(std::vector<int>& a, int n) {
FillVector(a);
steady_clock::time_point t1 = steady_clock::now();
SelectionSort(a);
steady_clock::time_point t2 = steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
return time_span.count();;
}


void Experiment(std::vector<int> &a, int n, double* means, double* stds) {
FillVector(a);
double* times = new double[10000];
std::cout << "\n\nFor b: " << std::endl;
for (int k{ 0 }; k <= 53; k++) std::cout << "-";
std::cout << "\nArgument" << " | " << "Average time" << " | " << "Standard deviation of time |" << std::endl;
for (int k{ 0 }; k <= 53; k++) std::cout << "-";
for (size_t i = 1000; i < 10000; i+=1000) {
std::cout << "\n[" << i << "]" << " ";
for (size_t j = 0; j < 10; ++j) {
times[j] = measureTime(a, i);
}
means = meanTime(times, i);
stds = stdTime(times, means);
}
}

int main() {
double* means = new double[10000];
double* stds = new double[10000];
int n{1000};
std::vector<int> a;
Experiment(a, n, means, stds);
return 0;
}
 

Kate

Administrator
Команда форума
Потому что длина вектора определяется в main и больше не меняется. Аналогичная задача - https://programmersforum.ru/showpost...77&postcount=9, но не хватает создания и заполнения вектора. Не нужно выделять по 10000 элементов для means, stds и times.
 
Сверху