package test;
import java.util.List;
public class BubbleSort {
public static <E extends Comparable<E>> E[] bubble(E[] es) {
for (int i = 0; i < es.length; i++) {
for (int j = 0; j < es.length - 1 - i; j++) {
E e;
if (es[j].compareTo(es[j + 1]) >= 0) {
e = es[j + 1];
es[j + 1] = es[j];
es[j] = e;
}
}
}
for (E e : es) {
System.out.print(e + "--");
}
System.out.println();
return es;
}
public static <E extends Comparable<E>> void show(E[] es) {
for (E e : es) {
System.out.print(e + "--" + "\n");
}
}
// 冒泡
public static int[] bubble(int[] lists) {
for (int i = 0; i < lists.length - 1; i++) {
for (int j = 0; j < lists.length - i - 1; j++) {
int temp;
if (lists[j] >= lists[j + 1]) {
temp = lists[j];
lists[j] = lists[j + 1];
lists[j + 1] = temp;
}
}
}
// print
for (int num : lists) {
System.out.print(num + "-");
}
return lists;
}
public static void main(String[] args) {
//测试
Integer[] nums = { 2, 31, 44, 1, 222, 33, 44, 15, 15, 24, 23 };
Double[] dNums = { 23.4, 23.5, 44.1, 2.44, 24.4 };
String[] ss = { "fs", "fssa", "asdadq" };
bubble(dNums);
bubble(ss);
bubble(nums);
}
}
冒泡泛型
最新推荐文章于 2024-05-06 10:05:46 发布