交换排序 - 冒泡排序
冒泡排序
原理:两个数相互交换,你比我大(小)就交换。
稳定性:稳定
时间复杂度:O(n^2)
===============================
public class MaoPaoSort {
public static void main(String[] args) {
Integer[] array = new Integer[]{2,1,4,10,12,1,65,11,94,-1};
int temp = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length-i-1; j++) {
if(array[j]>array[j+1])
{
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
ArrayUtil.out(array);
}
}
打印的工具类如下
public class ArrayUtil {
public static <T> void out(T[] array) {
for (T t : array) {
System.out.print(t + " ");
}
}
}
posted on 2017-07-26 17:49 一只小蜗牛12138 阅读(129) 评论(0) 收藏 举报
浙公网安备 33010602011771号