#include<iostream>
using namespace std;
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<fstream>
#include<iomanip>
int num[100000000];
int n = 10000;
void CreatNum(int n) {
ofstream ofs;
ofs.open("D:\\data.txt", ios::out);
srand(time(NULL));
int t;
for (int i = 0; i < n; i++) {
t = rand();
ofs << t << " ";
}
ofs.close();
}
void ReadFile() {
ifstream ifs;
ifs.open("D:\\data.txt", ios::in);
int t, i = 0;
while (ifs >> t) {
if (ifs.eof())
break;
num[i++] = t;
}
}
void bubble_sort(int num[], int n) {
//冒泡排序
int t;
for (int i = n - 1; i > 0;i--) {
for (int j = 0; j < i; j++) {
if (num[j] > num[j + 1]) {
t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
}
void print(int num[], int n) {
int count = 0;
for (int i = 0; i < n; i++) {
cout << num[i] << " ";
count++;
if (count % 10 == 0) {
cout << "\n";
}
}
}
int main() {
CreatNum(n);
cout << "n \t重复次数 \t 总时间 \t 每次排序时间\n";
cout << fixed << setprecision(8)<<setiosflags(ios::right);
for (int i = 0; i < 5; i++) {
ReadFile();
int count = 0;
double start = clock();
do {
count++;
bubble_sort(num, n);
} while (clock() - start < 1000);
double elapsedMills = (clock() - start);
cout << n;
cout << setw(10) << count;
cout << setw(20) << elapsedMills;
cout<< setw(18)<<elapsedMills/count<<"\n";
}
return 0;
}
[C ++]乱数を生成してファイルに書き込み、ファイルからデータを読み取って並べ替えます
おすすめ
転載: blog.csdn.net/weixin_48180029/article/details/114795933
おすすめ
ランキング