冒泡排序
冒泡排序(bubble sort),以升序来说,最后的排列一定是从小到大的。冒泡就是从最低端(数组末尾)开始,相邻的两个元素交换,把小的值放在前面,第一次冒泡就把最小的元素放到最顶端(数组首位)。冒泡排序是交换排序
冒泡的逻辑
1.双层遍历
2.外循环i,比如i=0,就把最小值放到array[0],即每次外循环结束后,都是第i个值(包含0哈)已经放好
3.内循环就是从末尾到i的交换,因为内循环位置肯定是大于等于0,最后一个元素总是j-1,而且j-1最终落到位置i上,因此j>i是一个条件
优缺点
最坏的情况就是升序或者降序排列,做降序或者升序的处理,判断与交换的总次数达到N的平方数
最好的情况也就是正序,外循环1次,内循环n次
最常见的冒泡例子
package com.java.arlgorithm.sort;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
/**
* @author
*/
@Slf4j
public class BubbleSortTest {
public static void main(String[] args) {
int[] array = new int[]{5, 6, 4, 3, 1, 6};
//printAll(array);
bubbleSort(array);
//printAll(array);
}
public static void bubbleSort(int[] array) {
if (array == null || array.length == 0) {
throw new NullPointerException();
}
int len = array.length;
for (int i = 0; i < len; i++) {
int tmp;
for (int j = len - 1; j > i; j--) {
if (array[j] < array[j - 1]) {
/* tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;*/
swap(array, j, j - 1);
}
printAll(array);
}
}
}
/* 形式参数的交换无法使得真实参数交换
public static void swap(int src, int des) {
int tmp = 0;
tmp = src;
src = des;
des = tmp;
}*/
public static void swap(int[] array, int src, int des) {
int tmp;
tmp = array[src];
array[src] = array[des];
array[des] = tmp;
}
public static void printAll(int[] array) {
log.info(Arrays.toString(array));
}
}
结果
2019-08-19 19:51:10,521 [main] INFO BubbleSortTest - [5, 6, 4, 3, 1, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [5, 6, 4, 1, 3, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [5, 6, 1, 4, 3, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [5, 1, 6, 4, 3, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 5, 6, 4, 3, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 5, 6, 4, 3, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 5, 6, 3, 4, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 5, 3, 6, 4, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 3, 5, 6, 4, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 3, 5, 6, 4, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 3, 5, 4, 6, 6]
2019-08-19 19:51:10,523 [main] INFO BubbleSortTest - [1, 3, 4, 5, 6, 6]
2019-08-19 19:51:10,524 [main] INFO BubbleSortTest - [1, 3, 4, 5, 6, 6]
2019-08-19 19:51:10,524 [main] INFO BubbleSortTest - [1, 3, 4, 5, 6, 6]
2019-08-19 19:51:10,524 [main] INFO BubbleSortTest - [1, 3, 4, 5, 6, 6]
Process finished with exit code 0
冒泡优化
package com.java.arlgorithm.sort;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
/**
* @author
*/
@Slf4j
public class BubbleSortTest {
public static void main(String[] args) {
int[] array = new int[]{5, 6, 4, 3, 1, 6};
int[] arrayOptmize = new int[]{1, 2, 3, 4, 5, 6};
//printAll(array);
//bubbleSort(array);
bubbleSortOptmize(arrayOptmize);
//printAll(array);
}
public static void bubbleSort(int[] array) {
if (array == null || array.length == 0) {
throw new NullPointerException();
}
int len = array.length;
for (int i = 0; i < len; i++) {
int tmp;
for (int j = len - 1; j > i; j--) {
if (array[j] < array[j - 1]) {
/* tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;*/
swap(array, j, j - 1);
}
printAll(array);
}
}
}
public static void bubbleSortOptmize(int[] array) {
if (array == null || array.length == 0) {
throw new NullPointerException();
}
int len = array.length;
for (int i = 0; i < len; i++) {
int tmp;
boolean flag=false;
for (int j = len - 1; j > i; j--) {
if (array[j] < array[j - 1]) {
/* tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;*/
swap(array, j, j - 1);
flag = true;
}
printAll(array);
if (flag == false) {
return;
}
}
}
}
/* 形式参数的交换无法使得真实参数交换
public static void swap(int src, int des) {
int tmp = 0;
tmp = src;
src = des;
des = tmp;
}*/
public static void swap(int[] array, int src, int des) {
int tmp;
tmp = array[src];
array[src] = array[des];
array[des] = tmp;
}
public static void printAll(int[] array) {
log.info(Arrays.toString(array));
}
}
结果
2019-08-19 20:13:15,069 [main] INFO BubbleSortTest - [1, 2, 3, 4, 5, 6]
原文地址:https://www.cnblogs.com/JuncaiF/p/11379276.html
时间: 2024-10-28 22:25:20