使用快速排序方法对数组a[]={11,34,6,2,21,7,15,9,10,13}升序排序,partition函数第一次被调用之后( )。
时间: 2023-10-08 16:08:20 浏览: 46
快速排序的 partition 函数是将数组分为左右两个部分,左边部分的元素都小于等于某个基准值,右边部分的元素都大于基准值。
在本题中,第一次调用 partition 函数时,可以选择数组的第一个元素作为基准值。调用后,数组会被分为两个部分,左边部分为{6, 2, 7, 9, 10, 11, 15, 34, 21, 13},右边部分为空。此时,基准值11已经找到了它在排好序数组中的位置,即数组中间位置,因此可以认为在 partition 函数第一次被调用之后,基准值已经被排好序了。
注意,这里的快速排序是升序排序,因此基准值左边的元素都小于等于基准值,右边的元素都大于基准值。
相关问题
编写程序,输出九九乘法表。 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
好的,以下是九九乘法表的代码:
```python
for i in range(1, 10):
for j in range(1, i+1):
print("{}*{}={}\t".format(j, i, i*j), end='')
print('\n')
```
输出:
```
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
```
希望能满足您的需求!
100个数据给数组int a[100]赋值,并在屏幕上输出(每行10个元素)。 2)使用函数 double isaver(int *p,int num) { } 求数组a中所有数组元素平均值。
好的,我可以回答你的问题。
首先,我会按照你的要求为数组a[100]赋值,并且输出数组元素:
```c++
#include <iostream>
using namespace std;
int main() {
int a[100];
for(int i=0; i<100; i++) {
a[i] = i+1;
}
for(int i=0; i<100; i++) {
cout << a[i] << " ";
if((i+1)%10 == 0) {
cout << endl;
}
}
return 0;
}
```
接下来,我会按照你给出的函数要求,写出求平均数的函数:
```c++
double isaver(int *p, int num) {
double sum = 0;
for(int i=0; i<num; i++) {
sum += *(p+i);
}
return sum/num;
}
```
最后,你可以调用这个函数来计算数组a中所有元素的平均值,并且输出结果:
```c++
#include <iostream>
using namespace std;
double isaver(int *p, int num) {
double sum = 0;
for(int i=0; i<num; i++) {
sum += *(p+i);
}
return sum/num;
}
int main() {
int a[100];
for(int i=0; i<100; i++) {
a[i] = i+1;
}
for(int i=0; i<100; i++) {
cout << a[i] << " ";
if((i+1)%10 == 0) {
cout << endl;
}
}
cout << "数组a的平均值为:" << isaver(a, 100) << endl;
return 0;
}
```
希望我的回答能够解决你的问题,如果还有其他问题,欢迎继续提问!
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)