冒泡泛型

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);

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值