6 Star 0 Fork 0

李嘉/作业

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
alg1.cpp 1.54 KB
Copy Edit Raw Blame History
李嘉 authored 2024-05-22 07:15 . 重命名 alg.cpp 为 alg1.cpp
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
using namespace std;
// 生成随机数据
vector<int> generateRandomData(int size, int minValue, int maxValue) {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(minValue, maxValue);
vector<int> data(size);
for (int i = 0; i < size; ++i) {
data[i] = dis(gen);
}
return data;
}
// 冒泡排序
void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
// 打印数组
void printArray(const vector<int>& arr) {
for (int num : arr) {
cout << num << " ";
}
cout << endl;
}
int main() {
int size = 10; // 数据集大小
int minValue = 1; // 数据最小值
int maxValue = 100; // 数据最大值
// 生成随机数据集
vector<int> data = generateRandomData(size, minValue, maxValue);
cout << "Original array: ";
printArray(data);
// 复制一份数据集用于验证排序后的结果
vector<int> dataCopy = data;
sort(dataCopy.begin(), dataCopy.end()); // 使用STL中的sort函数进行排序
// 对原始数据集进行冒泡排序
bubbleSort(data);
cout << "Sorted array (Bubble Sort): ";
printArray(data);
// 验证排序结果是否正确
if (data == dataCopy) {
cout << "Sorting is correct!" << std::endl;
}
else {
cout << "Sorting is incorrect!" << std::endl;
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/li-jia0706/task.git
git@gitee.com:li-jia0706/task.git
li-jia0706
task
作业
master

Search

0d507c66 1850385 C8b1a773 1850385