《动画演示(冒泡法)ppt课件.pptx》由会员分享,可在线阅读,更多相关《动画演示(冒泡法)ppt课件.pptx(40页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、冒泡排序法(升序)设计算法思路:1.如果对包含有n个数的数组an实行冒泡排序, 则需要n-1轮比较;2.第1轮比较时,从数组元素a0开始,依次比较a0和a1,a1和a2,a2和a3,.,an-2和an-1,并将大数后移,比较的结果是将数组最大数存入数组尾元素an-1中;3.第2轮比较时,同样从数组元素a0开始,依次比较a0和a1,a1和a2,a2和a3,.,an-3和an-2,并将大数后移,比较的结果是数组剩余元素的最大数存入元素an-2中;4.依此类推,第n-1轮比较时,比较a0和a1,将大数后移,存入元素a1中。排序完毕,数组元素按照升序排列排序完毕,数组元素按照升序排列。说明:大数后移是
2、指将每一对参与比较的元素的较大值存入后面的元素,较小值存入前面的元素。变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 注意:该注意:该PPT采用单步运行的方式演示了冒泡排序法采用单步运行的方式演示了冒泡排序法的实现过程,并实时显示各变量的值。的实现过程,并实时显示各变量的值。变量与变量的值下一步冒泡法演示(升序)程序
3、段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;23957 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n i jprintf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 定义变量,赋初值变量与变量的值下一步i=0时,表示数组元素的第一轮比较,该轮比较会将参与比较的两个数的较大数后移,比较的最终结果是数组中最大数移到最后。冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j
4、,t;23957 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i jprintf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步j=0时,比较变量s0和s1中值的大小,将较大数存入s1冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;23957 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i0j值
5、为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;32957 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i0j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=0时,比较变量s0和s1中值的大小,将较大数存入s1变量与变量的值下一步冒泡法演示(升序)程序段:#i
6、ncludemain()int s10=2,9,3,7,5, n=5, i,j,t;32957 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i1j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=1时,比较变量s1和s2中值的大小,将较大数存入s2变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;32957 s0 s1 s2 s3 s4for(i=0;in-1;i
7、+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i1j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=1时,比较变量s1和s2中值的大小,将较大数存入s2变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;92357 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i1j值为值为4printf(“%d %d %d %d %d n”,s0
8、, s1, s2, s3, s4); j=1时,比较变量s1和s2中值的大小,将较大数存入s2变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;92357 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i2j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=2时,比较变量s2和s3中值的大小,将较大数存入s3变量与变量的值下一步冒泡法演示(升序)程序段:#inclu
9、demain()int s10=2,9,3,7,5, n=5, i,j,t;92357 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i2j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=2时,比较变量s2和s3中值的大小,将较大数存入s3变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;92357 s0 s1 s2 s3 s4for(i=0;in-1;i+)fo
10、r(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i2j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=2时,比较变量s2和s3中值的大小,将较大数存入s3变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72359 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i3j值为值为4printf(“%d %d %d %d %d n”,s0, s1
11、, s2, s3, s4); j=3时,比较变量s3和s4中值的大小,将较大数存入s4变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72359 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i3j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=3时,比较变量s3和s4中值的大小,将较大数存入s4变量与变量的值下一步冒泡法演示(升序)程序段:#includema
12、in()int s10=2,9,3,7,5, n=5, i,j,t;72359 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i3j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=3时,比较变量s3和s4中值的大小,将较大数存入s4变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=
13、0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n0i4j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=4时,44为假,循环结束,该轮比较结束,数组最大数存入s4元素最大值存入s4变量与变量的值下一步i的值为1,开始第二轮比较,因s4已经存放的是数组最大数,故不参与比较,该轮比较只对s0s3的相邻元素比较大小,并将较大数后移冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0
14、;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i4j值为值为4printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步j=0时,比较变量s0和s1中值的大小,将较大数存入s1冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i0j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2,
15、 s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i0j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=0时,比较变量s0和s1中值的大小,将较大数存入s1变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t
16、;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i1j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=1时,比较变量s1和s2中值的大小,将较大数存入s2变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i
17、1j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=1时,比较变量s1和s2中值的大小,将较大数存入s2变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i2j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=2时,比较变量s2和s3中值的大小,将较
18、大数存入s3变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;72395 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i2j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=2时,比较变量s2和s3中值的大小,将较大数存入s3变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;723
19、95 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i2j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=2时,比较变量s2和s3中值的大小,将较大数存入s3变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n1i3j值为
20、值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); j=3时,33为假,循环结束,该轮比较结束后,次大数存入s3变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n2i3j值为值为3printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); i的值变为2,开始第三轮比较,同理,该轮比较会找
21、出s0s2中的最大数,并存入s2中变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n2i0j值为值为2printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4
22、for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n2i0j值为值为2printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n2i1j值为值为2printf(“%d %d %d %d %d n”,s0, s1, s2, s3,
23、s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n2i1j值为值为2printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1
24、;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n2i2j值为值为2printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n3i2j值为值为2printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值
25、下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n3i0j值为值为1printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;
26、jsj+1) t=sj;sj=sj+1;sj+1=t;5n3i0j值为值为1printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n3i1j值为值为1printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)
27、程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n4i1j值为值为1printf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;
28、sj=sj+1;sj+1=t;5n4i1jprintf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 值为值为1变量与变量的值下一步冒泡法演示(升序)程序段:#includemain()int s10=2,9,3,7,5, n=5, i,j,t;52397 s0 s1 s2 s3 s4for(i=0;in-1;i+)for(j=0;jsj+1) t=sj;sj=sj+1;sj+1=t;5n4i1jprintf(“%d %d %d %d %d n”,s0, s1, s2, s3, s4); 值为值为1使用冒泡法排序时,当数组中有N个元素时,需要比较N-1轮,第一轮比较会将最大数(升序)或最小数(降序)置于数组最后一个元素中,在第二轮比较时,数组最后一个元素不参与比较,剩余元素中的最大数(升序)或最小数(降序)置于数组的倒数第二个元素中,依此类推,可实现数组元素的升序或降序排列。