您好,欢迎访问一九零五行业门户网

Bubble Sort,bubblesort_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 冒泡排序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&......余下全文>>
http://www.bkjia.com/phpjc/908125.htmlwww.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...
其它类似信息

推荐信息