本文共 654 字,大约阅读时间需要 2 分钟。
package myFirstApp;
//冒泡排序demo
public class BubbleSort {
public static void main(String[] args) { BubbleSort bs = new BubbleSort(); int l[] = new int[10]; for(int i=0;i<10;i++) { l[i] = (int) (100 * Math.random()); } System.out.print("list排序前为:"); bs.printList(l); System.out.print("list排序后为:"); bs.printList(bs.sortNumber(l));}//打印list内容public void printList(int[] list) { for (int x=0;xlst[x]) { int t = lst[x-1]; lst[x-1] = lst[x]; lst[x] = t; complete = false; //排序一轮下来,是否仍有未排序的相邻数字 } } if(complete) { isSorted = true; } } return lst;}
}
输出结果是:
list排序前为:15 30 83 0 5 68 2 24 59 76 list排序后为:0 2 5 15 24 30 59 68 76 83转载地址:http://urezi.baihongyu.com/