使用new运算符创建具有10个元素的一维数组,实现以下操作(20分): (1) 编写子函数bubblesort(int arr[],int x)实现用冒泡排序算法将数组升序排序。 (2) 统计在排序过程中元素有多少次交换。
时间: 2023-05-02 15:01:29 浏览: 68
这道题目的意思是使用新的运算符创建一个具有10个元素的一维数组,实现以下操作(20分钟):
(1)编写子函数bubblesort(int arr[],int x),实现用冒泡排序算法将数组升序排列。
(2)统计在排序过程中元素有多少次交换。
注:这里的bubblesort(int arr[],int x)就是冒泡排序算法,该算法可以将数组升序排列。而第二个操作则是要求你在排序过程中统计元素交换的次数。
相关问题
一维数组元素排序,要求如下: 使用选择法,冒泡法实现 使用函数调用的方式
以下是使用选择法实现一维数组元素排序的函数调用方式:
```c++
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
for (int i=0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
```
以下是使用冒泡法实现一维数组元素排序的函数调用方式:
```c++
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(arr[j], arr[j+1]);
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: \n";
for (int i=0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
```
一维数组元素排序,要求如下:分别 使用选择法,冒泡法实现 使用函数调用的方式
选择排序的函数实现如下:
```c++
void selectionSort(int arr[], int n){
int i, j, min_idx;
for (i = 0; i < n-1; i++){
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
```
冒泡排序的函数实现如下:
```c++
void bubbleSort(int arr[], int n){
int i, j;
for (i = 0; i < n-1; i++){
for (j = 0; j < n-i-1; j++){
if (arr[j] > arr[j+1])
swap(arr[j], arr[j+1]);
}
}
}
```
其中,`swap` 函数可以使用 `<algorithm>` 头文件中的 `swap` 函数实现。
函数调用示例:
```c++
int main(){
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
// 选择排序
selectionSort(arr, n);
cout << "Selection Sort: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
// 冒泡排序
bubbleSort(arr, n);
cout << "Bubble Sort: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)