Как отметили @Tom De Coninck и @ p js, ваша проблема в размере выборки, и, как вы упомянули в комментарии, если вы увеличите размер выборки, для ее генерации потребуется много времени.
Моя идея заключается в том, чтобы сгенерировать данные с помощью программного обеспечения C ++ (намного быстрее), а затем построить их с помощью Python. С этим я могу сгенерировать и построить 10000 запусков менее чем за 20 секунд.
Вот мой код (алгоритм быстрой сортировки был адаптирован из C ++ Программа для быстрой сортировки - GeeksforGeeks )
Код C ++ генерирует out.txt
, содержащий общее количество сравнений для каждого прогона, разделенных новой строкой. Сценарий Python считывает строки и строит их (с различными размерами сегментов, в зависимости от состояния назначения)
Генератор C ++
// g++ ./LVQuickSort.cpp -o lvquicksort
#include <iostream>
#include <fstream>
#include <cstdlib>
int ARRAY_TO_SORT_SIZE = 10000;
int RUNS = 10000;
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high, int &comps)
{
int pivot = arr[(rand() % (high - low)) + low];
int i = low - 1;
for (int j = low; j <= high - 1; j++)
{
comps++;
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high, int &comps)
{
if (low < high)
{
int pi = partition(arr, low, high, comps);
quickSort(arr, low, pi - 1, comps);
quickSort(arr, pi + 1, high, comps);
}
}
std::ofstream file;
void write_comps_to_file(int comps)
{
file << comps << std::endl;
}
int main()
{
file.open("./out.txt", std::fstream::trunc);
for (size_t i = 0; i < RUNS; i++)
{
int *arr = (int *)malloc(sizeof(int) * ARRAY_TO_SORT_SIZE);
for (int i = 0; i < ARRAY_TO_SORT_SIZE; i++)
arr[i] = rand() % 1000;
int comps = 0;
if (i % (RUNS / 50) == 0)
std::cout << i << "/" << RUNS<< std::endl;
quickSort(arr, 0, ARRAY_TO_SORT_SIZE - 1, comps);
write_comps_to_file(comps);
}
file.close();
}
Python плоттер
import matplotlib.pyplot as plt
f = open('out.txt', 'r')
binCounts = [10, 50, 100, 200, 1000, 5000]
for binCount in binCounts:
vals = []
f.seek(0)
for line in f.readlines():
vals.append(int(line))
plt.hist(vals, bins=binCount)
plt.gca().set(title='|S|=10^4 | Runs=10^4', xlabel='Comparisons', ylabel='Runs')
plt.savefig(f'out{binCount}.png')
plt.close()