数组排序-冒泡排序

来源:互联网 发布:京东量化 招聘java 编辑:程序博客网 时间:2024/04/28 04:03

冒泡排序外层循环控制循环的次数,里层的循环对值进行判断,外层每次循环,找出最大(或最小)值。

具体过程:


代码实现过程:

public class Bubble {static int s;public static void main(String[] args) {long startTime = System.currentTimeMillis();int[] a = { 2, 4, 7, 3, 21, 9, 8 };for (int i = 0; i < a.length - 1; i++) {for (int j = 0; j < a.length - 1 - i; j++) {if (a[j] > a[j + 1]) {s = a[j];a[j] = a[j + 1];a[j + 1] = s;}}}for (int i = 0; i < a.length; i++) {System.out.print(a[i] + " ");}long endTime = System.currentTimeMillis();System.out.println();System.out.println("程序运行时间:"+(endTime-startTime)+"ms");}}

0 0