#include<bits/stdc++.h>
using namespace std;
#include <chrono>
using namespace std::chrono;
#define ll long long int
#define ull unsigned ll
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define pb push_back
#define vll vector<ll>
#define vp vector<pair<ll, ll>>
#define all(v) v.begin(), v.end()
#define hcf(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/hcf(a,b)
#define fori(n) for (ll i = 0; i < n; ++i)
#define forj(n) for (ll j = 0; j < n; ++j)
#define fork(n) for (ll k = 0; k < n; ++k)
bool isPrime (ll a) { if (a < 2) return false; for (ll i = 2; i * i <= a; i++) if (a % i == 0) return false; return true; }
const int M = 1e9 + 7;
void bubble_sort(vll &v) {
int n = v.size();
fori(n - 1) {
forj (n-i-1) {
if (v[j]>v[j+1]) swap(v[j], v[j + 1]);
}
}
}
vll generate_case(int n, string type) {
vll v(n);
if (type == "best") {
iota(v.begin(), v.end(), 1);
} else if (type == "worst") {
iota(v.begin(), v.end(), 1);
reverse(v.begin(), v.end());
} else {
fori(n) v[i] = rand() % (10 * n);
}
return v;
}
int main() {
srand(time(0));
ofstream fout("bubble_data.dat");
fout << "#Size Best(ms) Average(ms) Worst(ms)\n";
for (int size = 100; size <= 2000; size += 200) {
cout << "Running size: " << size << endl;
fout << size << " ";
for (string type : {"best", "avg", "worst"}) {
vll v = generate_case(size, type);
auto start = high_resolution_clock::now();
bubble_sort(v);
auto end = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end - start).count();
fout << duration / 1000.0 << " ";
}
fout << "\n";
}
fout.close();
// Write gnuplot script
ofstream gp("plot_script.gp");
gp << "set title 'Bubble Sort: Input Size vs Time'\n";
gp << "set xlabel 'Input Size (n)'\n";
gp << "set ylabel 'Time (ms)'\n";
gp << "set grid\n";
gp << "set key left top\n";
gp << "plot 'bubble_data.dat' using 1:2 with linespoints title 'Best', \\\n";
gp << " 'bubble_data.dat' using 1:3 with linespoints title 'Average', \\\n";
gp << " 'bubble_data.dat' using 1:4 with linespoints title 'Worst'\n";
gp.close();
// Run gnuplot
system("gnuplot -persist plot_script.gp");
return 0;
}