5.下面的程序段中, n为正整数,则最后一行的语句频度在最坏情况下是( ) for(i=n-1;i>=1;i--) for(j=1;j<=i;j++) if (A[j]>A[j+1]) A[j]与A[j+1]对换;讲出解题过程
时间: 2023-04-01 18:04:04 浏览: 68
根据程序段,可以看出这是一个冒泡排序算法。在最坏情况下,即数组A中的元素是逆序排列的情况下,每次内循环都需要比较和交换相邻的两个元素,因此内循环的频度为n-1+n-2+...+2+1=(n-1)*n/2。而外循环的频度为n-1。因此最后一行的语句频度在最坏情况下是(n-1)*(n-1)/2,即O(n^2)。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)