冒泡排序
2019-08-24 本文已影响0人
cc_And
package com.cc.handlerdemo;
/*
*@Auther:host_And
*@Date: 2019/8/24
*@Time:18:05
*@Description:${冒泡排序}
* */public class Test {
public static void main(String[] args) {
int[] array = {1, 13, 72, 9, 22, 4, 6, 781, 29, 2, 6564665, 0, 556, 87452};
System.out.println("排序后的结果是:");
//倒序
BubbleSort(array, true);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
/**
* 是否倒叙
*
* @param array
* @param reversed
*/
private static void BubbleSort(int[] array, Boolean reversed) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (reversed) {
//倒序
if (array[j] < array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
} else {
//正序
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
}
打印结果 :
排序后的结果是:
6564665 87452 781 556 72 29 22 13 9 6 4 2 1 0