复杂数据的冒泡排序

前段时间,有个排序的功能就是做离我最近的那个,一可以想用冒泡,之前用冒泡都是简单的数据结构,一般都是数组来实现。在项目中往往不会这么简单,数据结构复杂得多。

本人在项目中使用的还不算太复杂,直接看代码把

		/**
		 * list里面放个类 ,PublishVo是个泛型类,里面继续放一个hashmap
		 * 这里排序是拿的 PublishVo里面的属性 Count 来排序
		 * 并不是 PublishVo 属性里面 publishTheme 里面的值
		 * 
		 * publishVos2为临时变量,暂时存储数据用于交换的
		 * 
		 * 这里需要说明的 list 用于交换是set()方法 注意
		 * 
		 * 本案例在项目中拿出来的·
		 */
		List<PublishVo<HashMap<String, Object>>> publishVos2 = new ArrayList<PublishVo<HashMap<String, Object>>>();
		int size2 = publishVos.size();
		for (int i = 0; i < size2 - 1; i++) {
			for (int j = i + 1; j < size2; j++) {
				if (publishVos.get(i).getCount() < publishVos.get(j).getCount()) {
					publishVos2.add(publishVos.get(i));
					publishVos.set(i, publishVos.get(j));
					publishVos.set(j, publishVos2.get(0));
					//需要清楚里面的数据 不然第二次拿的时候值就不对了
					publishVos2.clear();
				}
			}
		}
		return publishVos;

注释都有,基本都看得懂。

package sorter;

import java.util.Comparator;

public class BubbleSorter implements Sorter {

    @Override
    public <T extends Comparable<T>> void sort(T[] list) {
        boolean swapped = true;
        for (int i = 1, len = list.length; i < len && swapped; ++i) {
            swapped = false;
            for (int j = 0; j < len - i; ++j) {
                if (list[j].compareTo(list[j + 1]) > 0) {
                    T temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                    swapped = true;
                }
            }
        }
    }

    @Override
    public <T> void sort(T[] list, Comparator<T> comp) {
        boolean swapped = true;
        for (int i = 1, len = list.length; i < len && swapped; ++i) {
            swapped = false;
            for (int j = 0; j < len - i; ++j) {
                if (comp.compare(list[j], list[j + 1]) > 0) {
                    T temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                    swapped = true;
                }
            }
        }
    }
    public static void main(String[] args) {
    	Integer score[] = { 67, 69,11,98, 75, 87, 89, 10, 99, 100 };
    	BubbleSorter h = new BubbleSorter();
    	h.sort(score);
    	for (int i = 0; i < score.length; i++) {
			System.out.println(score[i]);
		}
	}
}

以上有个接口

public interface Sorter {
	/**
	 * 排序
	 * 
	 * @param list
	 *            待排序的数组
	 */
	public <T extends Comparable<T>> void sort(T[] list);

	/**
	 * 排序
	 * 
	 * @param list
	 *            待排序的数组
	 * @param comp
	 *            比较两个对象的比较器
	 */
	public <T> void sort(T[] list, Comparator<T> comp);
}

还有其他方法 可以参考下

public class Sort {

	public static void main(String[] args) {
		mao1();
	}

	public static void mao() {
		int score[] = { 67, 69,11,98, 75, 87, 89, 10, 99, 100 };
		for (int i = 0; i < score.length - 1; i++) { // 最多做n-1趟排序
			// 对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
			for (int j = 0; j < score.length - i - 1; j++) {
				if (score[j] > score[j + 1]) { // 把小的值交换到后面
					int temp = score[j];
					score[j] = score[j + 1];
					score[j + 1] = temp;
				}
			}
			System.out.print("第" + (i + 1) + "次排序结果:");
			for (int a = 0; a < score.length; a++) {
				System.out.print(score[a] + "\t");
			}
			System.out.println("");
		}
		System.out.print("最终排序结果:");
		for (int a = 0; a < score.length; a++) {
			System.out.print(score[a] + "\t");
		}
	}
	
	
	public static void mao1() {
		int score[] = { 67, 69,11,98, 75, 87, 89, 10, 99, 100 };
		for (int i = 0; i < score.length - 1; i++) { // 最多做n-1趟排序
			for (int j = i+1; j < score.length; j++) {
				if (score[i] > score[j]) { // 把大的值交换到后面
					int temp = score[j];
					score[j] = score[i];
					score[i] = temp;
				}
			}
			System.out.print("第" + (i + 1) + "次排序结果:");
			for (int a = 0; a < score.length; a++) {
				System.out.print(score[a] + "\t");
			}
			System.out.println("");
		}
		System.out.print("最终排序结果:");
		for (int a = 0; a < score.length; a++) {
			System.out.print(score[a] + "\t");
		}
	}
}
mao的感觉好一点 直观  mao1是个人比较习惯用的一种

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值