标签:arrays ++ 简单 技术 compareto rabl inf mic com
package com.sort;
/*--------------
* Author:Real_Q
* Date:2021-01-06
* Time:10:01
* Description:冒泡排序
* {4,5,6,3,2,1};
---------------*/
public class BubbleSort {
//排序
public static void bubbleSort(Comparable[] comparables) {
//冒泡排序 小------>大
//排序次数 i
for (int i = comparables.length - 1; i > 0; i--) {
//比较大小次数及交换次数 i
for (int j = 0; j < i; j++) {
if (Comparable(comparables[j], comparables[j + 1])) {
exchange(comparables, j, j +1);
}
}
}
}
//比较大小
public static boolean Comparable(Comparable comparable1, Comparable comparable2) {
return comparable1.compareTo(comparable2) > 0;
}
//交换元素
public static void exchange(Comparable[] comparable, int leftIndex, int rightIndex) {
Comparable temp;
temp = comparable[leftIndex];
comparable[leftIndex] = comparable[rightIndex];
comparable[rightIndex] = temp;
}
}
import java.util.Arrays;
import static com.sort.BubbleSort.bubbleSort;
public class TestBubble {
public static void main(String[] args) {
Integer[] integers = {4,6,8,7,9,2,10,1};
bubbleSort(integers);
System.out.println(Arrays.toString(integers));
}
}
标签:arrays ++ 简单 技术 compareto rabl inf mic com
原文地址:https://www.cnblogs.com/RealQ/p/14253388.html