Java冒泡排序

来源:互联网 发布:苹方字体 mac版下载 编辑:程序博客网 时间:2024/06/05 14:54

每次从头开始,前后比较,每次获取一个最大或者最小值。到最后冒泡排序。


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);
}


}
0 0
原创粉丝点击