c语言冒泡排序数组指针,c语言冒泡排序,指针,数组

冒泡排序算法的运作如下:

比较相邻的元素。如果第一个比第二个大,就交换他们两个。

对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。

针对所有的元素重复以上的步骤,除了最后一个。

持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

时间复杂度

若文件的初始状态是正序的,一趟扫描即可完成排序。所需的关键字比较次数

0818b9ca8b590ca3270a3433284dd417.png

和记录移动次数

0818b9ca8b590ca3270a3433284dd417.png

均达到最小值:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

所以,冒泡排序最好的时间复杂度为

0818b9ca8b590ca3270a3433284dd417.png

若初始文件是反序的,需要进行

0818b9ca8b590ca3270a3433284dd417.png

趟排序。每趟排序要进行

0818b9ca8b590ca3270a3433284dd417.png

次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

冒泡排序的最坏时间复杂度为

0818b9ca8b590ca3270a3433284dd417.png

综上,因此冒泡排序总的平均时间复杂度为

0818b9ca8b590ca3270a3433284dd417.png

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

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;

}

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值