我也是菜鸟,我喜欢这样写,两个FOR循环外面的计算冒泡次数里面的进行比较
解决方案 »
- 求用DWR向后台传一个对象,做一个保存数据的方法!
- sqlstate=42908
- jsp页面 引用 servlet生成的验证码 路径该怎样写啊
- xfire框架生成的webservice服务端被调用时报错,请高手帮忙看一下
- ★★★问:全中国学JSF的超过100人吗?
- 救命!某大公司程序员笔试题!答中有重奖!超难算法
- JTable不能显示列名,为什么?帮帮忙.
- 请到http://220.114.108.6:8080/jxc添加条记录
- 用java向SQLServer中插入一条记录,其中id(自动增长)这个字段该如何写.
- ssh编写文件下载为啥不能显示
- 用struts+hibernate没错用struts+hibernate+spring就这样,为什么呢???
- 飞思在线的网站是不是挂了?
import java.util.*;
public class bubblesort
{
public static int[] a=new int[10];
public static void bubblesort(int num)
{ int temp;
for(int i=num;i>1;i--)
for(int j=0;j<num-1;j++)
if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
for(int index=0;index<10;index++)
{
a[index]=Integer.parseInt(input.next());
}
input.close();
bubblesort.bubblesort(10);
for(int index=0;index<10;index++)
System.out.println(a[index]);
}
};
code}
我也是刚学的,代码写的有些乱请见谅啊
for(int j=0;j <num-1;j++)
if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
你把冒泡的算法看好了,
自己跟到程序走一边,
在for 里,
搞的几次,就晓的了呀
thank you all of person!!
就下面这样
int temp=0;
for(int i=number;i>1;i--)
for(int j=0;j <number-1;j++)
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
2. * 冒泡排序算法
3. */
4. public class BubbleSort {
5. public static void sort(int[] values) {
6. int temp;
7. for (int i = 0; i < values.length; ++i) {
8. for (int j = 0; j < values.length - i - 1; ++j) {
9. if (values[j] > values[j + 1]) {
10. temp = values[j];
11. values[j] = values[j + 1];
12. values[j + 1] = temp;
13. }
14. }
15. }
16. }
17. }