PHP头条
热点:

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: 

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

 


冒泡排序c

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

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 冒泡法排序 */
for(j=0;j<10-i-1;j++)
if(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;i<10;i++)
printf("%-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<9不再成立退出比较。
对于n个数,只需要进行n-1次外循环的两两比较就完成排序。
至于按降序排列只需将if(a[j]>a[j+1])改为if(a[j]<a[j+1])即可。

-------------------------------------------------------------------
/* 用改进型冒泡排序法对一维整型数组中的十个数升序排序 */
#include <stdio.h>
#include <stdlib.h>
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 改进型冒泡法排序 */
{ flag=0;
for(j=0;j<10-i-1;j++)
if(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 <stdio.h>
#include <stdlib.h>

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 冒泡法排序 */
for(j=0;j<10-i-1;j++)
if(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;i<10;i++)
printf("%-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<9不再成立退出比较。
对于n个数,只需要进行n-1次外循环的两两比较就完成排序。
至于按降序排列只需将if(a[j]>a[j+1])改为if(a[j]<a[j+1])即可。

-------------------------------------------------------------------
/* 用改进型冒泡排序法对一维整型数组中的十个数升序排序 */
#include <stdio.h>
#include <stdlib.h>
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 改进型冒泡法排序 */
{ flag=0;
for(j=0;j<10-i-1;j++)
if(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.phpzy.comtrue/php/28707.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 largest 7. . . . 1, 7, 8 2, 6, 7 3, 5, 6 4, 4, 5 5, 3, 4 6, 2,...

相关文章

PHP之友评论

今天推荐