首页 > 后端开发 > php教程 > Bubble Sort,bubblesort_PHP教程

Bubble Sort,bubblesort_PHP教程

WBOY
发布: 2016-07-13 10:14:49
原创
865 人浏览过

Bubble Sort,bubblesort

8 numbers. Sort as ascend.

 

1st loop, compare 7 times (for 8 numbers), and found the largest 8.

2nd loop, compare 6 times (for 7 numbers), and found the largest 7.

. . .

 

1, 7, 8

2, 6, 7

3, 5, 6

4, 4, 5

5, 3, 4

6, 2, 3

7, 1, 2

 

In conclusion: For sorting 8 numbers, we need an outer loop of 7 times, each time for finding a largest number; and an inner loop from comparing 7 times to comparing 1 time (as in the center column).

 

Implementation in PHP: 

<span> 1</span> <?<span>php
</span><span> 2</span> <span>/*</span><span> bubble sort: 
</span><span> 3</span> <span>    1. operate directly on the input array (&), not on a copy
</span><span> 4</span> <span>    2. sort as ascend
</span><span> 5</span> 
<span> 6</span> <span>    a is array
</span><span> 7</span> <span>    m is length of a
</span><span> 8</span> <span>    n is times of outer loop, n-i is times of comparing for each outer loop
</span><span> 9</span> <span>    i/j is for-loop counter
</span><span>10</span> <span>    w is for value swap
</span><span>11</span> <span>*/</span>
<span>12</span> <span>function</span> sortBubble(&<span>$a</span><span>){
</span><span>13</span>     <span>$m</span> = <span>count</span>(<span>$a</span><span>);
</span><span>14</span>     <span>$n</span> = <span>$m</span> - 1<span>;
</span><span>15</span>     <span>for</span>(<span>$i</span>=0; <span>$i</span><<span>$n</span>; <span>$i</span>++<span>){
</span><span>16</span>         <span>for</span>(<span>$j</span>=0; <span>$j</span><<span>$n</span>-<span>$i</span>; <span>$j</span>++<span>){
</span><span>17</span>             <span>if</span>(<span>$a</span>[<span>$j</span>] > <span>$a</span>[<span>$j</span>+1<span>]){
</span><span>18</span>                 <span>$w</span> = <span>$a</span>[<span>$j</span><span>];
</span><span>19</span>                 <span>$a</span>[<span>$j</span>] = <span>$a</span>[<span>$j</span>+1<span>];
</span><span>20</span>                 <span>$a</span>[<span>$j</span>+1] = <span>$w</span><span>;
</span><span>21</span> <span>            }
</span><span>22</span>             <span>else</span><span>{
</span><span>23</span>                 <span>//</span><span> do nothing</span>
<span>24</span> <span>            }
</span><span>25</span> <span>        }
</span><span>26</span>         <span>//</span><span> see the results after each outer loop
</span><span>27</span> <span>        // echo implode(', ', $a).'<br />';</span>
<span>28</span> <span>    }
</span><span>29</span> <span>}
</span><span>30</span> 
<span>31</span> <span>$arr</span> = <span>array</span>(9, 5, 2, 7, 3<span>);
</span><span>32</span> sortBubble(<span>$arr</span><span>);
</span><span>33</span> <span>echo</span> <span>implode</span>(', ', <span>$arr</span><span>);
</span><span>34</span> 
<span>35</span> <span>//</span><span> 2, 3, 5, 7, 9</span>
<span>37</span> ?>
登录后复制

 

冒泡排序c

冒泡排序详细注释:
/* 用冒泡排序法对一维整型数组中的十个数升序排序 */
#include
#include

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;ifor(j=0;jif(a[j]>a[j+1])
{t=a[j];/* 交换a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;iprintf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
其中i=0时:
j从0开始a[0],a[1]比较大小,把其中的较大者给a[1],然后j++,a[1]和a[2]再比较,再把两者中的
较大者给a[2],这样a[0],a[1],a[2]中的最大者已经交换到a[2]中,这个过程继续,直到j=10-i-1=9这样
a[9]中的为10个数中的最大数。
然后i=1时:
由于最大数已找到并放到a[9]中,所以这一次循环j最大只需到10-i-1=8,即a[8]即可,再次从j=0开始a[j]和a[j+1]两两比较交换,最后次大数放到a[8]中
然后i++,继续...
当i=9时已经过9次两两比较完成所有排序,i对于n个数,只需要进行n-1次外循环的两两比较就完成排序。
至于按降序排列只需将if(a[j]>a[j+1])改为if(a[j]
-------------------------------------------------------------------
/* 用改进型冒泡排序法对一维整型数组中的十个数升序排序 */
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{ flag=0;
for(j=0;jif(a[j]>a[j+1])
{ t=a[j]; /* 交换a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&......余下全文>>
 

冒泡排序c

冒泡排序详细注释:
/* 用冒泡排序法对一维整型数组中的十个数升序排序 */
#include
#include

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;ifor(j=0;jif(a[j]>a[j+1])
{t=a[j];/* 交换a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;iprintf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
其中i=0时:
j从0开始a[0],a[1]比较大小,把其中的较大者给a[1],然后j++,a[1]和a[2]再比较,再把两者中的
较大者给a[2],这样a[0],a[1],a[2]中的最大者已经交换到a[2]中,这个过程继续,直到j=10-i-1=9这样
a[9]中的为10个数中的最大数。
然后i=1时:
由于最大数已找到并放到a[9]中,所以这一次循环j最大只需到10-i-1=8,即a[8]即可,再次从j=0开始a[j]和a[j+1]两两比较交换,最后次大数放到a[8]中
然后i++,继续...
当i=9时已经过9次两两比较完成所有排序,i对于n个数,只需要进行n-1次外循环的两两比较就完成排序。
至于按降序排列只需将if(a[j]>a[j+1])改为if(a[j]
-------------------------------------------------------------------
/* 用改进型冒泡排序法对一维整型数组中的十个数升序排序 */
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{ flag=0;
for(j=0;jif(a[j]>a[j+1])
{ t=a[j]; /* 交换a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&......余下全文>>
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/908125.htmlTechArticleBubble Sort,bubblesort 8 numbers. Sort as ascend. 1st loop, compare 7 times (for 8 numbers), and found the largest 8. 2nd loop, compare 6 times (for 7 numbers), and found the lar...

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板