您的位置:首页 > 编程语言 > Java开发

Java冒泡排序

2016-10-14 15:52 302 查看
每次从头开始,前后比较,每次获取一个最大或者最小值。到最后冒泡排序。

package test;

public class BubbleSort {

static int[] array = { 1, 2, 3, 4, 67, 8, 9, 6, 4,5,6,9,8,5,4,2,1,3,5,4,123,1,2,45,1,66,45,45,12,44,55,44,54,55,4 };
static int count;
static int times;

public static void main(String[] args) {
sort(array);
}

public static void sort(int[] array) {
times = array.length;
while (times>0) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
count++;
times--;
if (array[i] > array[j]) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;

}
}
}

}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
System.out.print(", ");
}
System.out.println("");
System.out.println("copare times:"+count);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: