建立10000个随机整数数组,排序后输出排序结果。c++
时间: 2023-06-20 22:04:43 浏览: 55
可以使用C++中的STL库中的sort函数来进行排序,代码如下:
```c++
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
int main() {
const int N = 10000;
int arr[N];
// 生成随机数
srand((unsigned)time(NULL));
for (int i = 0; i < N; i++) {
arr[i] = rand() % 100000;
}
// 排序
sort(arr, arr + N);
// 输出排序结果
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
首先生成一个长度为10000的随机整数数组,然后使用sort函数进行排序,最后输出排序结果。
相关问题
用c++完成建立10000个随机整数数组,运用选择排序
好的,以下是用 C++ 完成建立 10000 个随机整数数组,并使用选择排序算法的示例代码:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ARRAY_SIZE = 10000;
// 生成随机整数数组
void generateRandomArray(int arr[], int size) {
srand(time(NULL)); // 设置随机数种子
for (int i = 0; i < size; i++) {
arr[i] = rand() % 100000; // 生成 0 到 99999 的随机整数
}
}
// 选择排序算法
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
}
}
int main() {
int arr[ARRAY_SIZE];
generateRandomArray(arr, ARRAY_SIZE); // 生成随机整数数组
selectionSort(arr, ARRAY_SIZE); // 使用选择排序算法排序
for (int i = 0; i < ARRAY_SIZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
该程序中,我们首先生成了一个包含 10000 个随机整数的数组,然后使用选择排序算法对其进行排序,并最终输出排序后的结果。请注意,由于数组中的整数是随机生成的,每次运行程序时,得到的结果都可能不同。
2.用C++完成两种排序算法:建立10000个随机整数数组,排序后输出排序结果。排序算法:选择排序,冒泡排序,插入排序,计数排序,基数排序,快速排序,归并排序,堆排序,桶排序,希尔排序,锦标赛排序
以下是用 C++ 实现的两种排序算法,选择排序和快速排序。建立一个包含10000个随机整数的数组,然后对其进行排序,最后输出排序结果。
选择排序:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
swap(arr[i], arr[min_idx]);
}
}
int main() {
const int n = 10000;
int arr[n];
srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000;
}
selectionSort(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
快速排序:
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
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) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
const int n = 10000;
int arr[n];
srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000;
}
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
注意:以上代码是对随机生成的整数进行排序,如果是对其他类型的数据进行排序,则需要修改代码中的数据类型和比较方式。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)