在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
冒泡排序算法的运作如下:
时间复杂度若文件的初始状态是正序的,一趟扫描即可完成排序。所需的关键字比较次数
和记录移动次数
均达到最小值:
,
。
所以,冒泡排序最好的时间复杂度为
。
若初始文件是反序的,需要进行
趟排序。每趟排序要进行
次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值:
冒泡排序的最坏时间复杂度为
。
综上,因此冒泡排序总的平均时间复杂度为
。
2. Use pointer to complete the assignment. define array for three integers. Write three functions, which are input(), deal(), print()The input() function needs to complete three number's input. The deal() function needs to put the smallest onto the first position, put the biggest one onto the end of the sequence. The print() function needs to print the result. #include<stdio.h>
int input(int* a);
int output(int* a);
int deal(int *);
int main()
{
int array[3];
input(array);
deal(array);
output(array);
return 0;
}
int input(int* a)
{
int i;
for(i=0;i<3;i++)
{
scanf("%d",&*(a+i));
}
return 0;
}
int deal(int *a)
{
int min,i,j;
for(i=0;i<3-1;i++)
for(j=0;j<2-i;j++)
if(*(a+j)>*(a+j+1))
{
min=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=min;
}
return 0;
}
int output(int* a)
{
int i;
for(i=0;i<3;i++)
printf("%d ",*(a+i));
return 0;
}
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论