[练习][Java]对象列排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/devil_129/article/details/78395132

闲着DT,写了个排序,望大家多多指点
ps:想写个多叉堆没写完,暂时写个二叉堆

package com.gliolio.common.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 对象List排序工具
 * 
 * @author Devil_129
 */

public class SortUtils {
	/**
	 * 排序方式
	 * 
	 * @author Devil_129
	 * @param <T>
	 *            泛型
	 */
	public interface Order<T> {
		/**
		 * 比较是否A在B前面
		 * 
		 * @param a
		 * @param b
		 * @return
		 */
		public boolean isAInFrontOfB(T a, T b);
	}

	/**
	 * 冒泡排序
	 * 
	 * @param <T>
	 *            泛型
	 * @param data
	 *            数据列
	 * @param orderFunc
	 *            排序方式
	 * @return 有序数据列
	 */
	public static <T> List<T> BubbleSort(List<T> data, Order<T> orderFunc) {
		List<T> result = new ArrayList<T>(data);
		for (int i = 0; i < result.size(); i++) {
			for (int j = i + 1; j < result.size(); j++) {
				if (orderFunc.isAInFrontOfB(result.get(j), result.get(i))) {
					SortUtils.exchange(result, i, j);
				}
			}
		}
		return result;
	}

	/**
	 * 二叉堆排序
	 * 
	 * @param data
	 *            数据列
	 * @param orderFunc
	 *            排序方式
	 * @return 有序数据列
	 */
	public static <T> List<T> BinaryHeapSort(List<T> data, Order<T> orderFunc) {
		List<T> heapList = SortUtils.buildHeap(data, orderFunc, 2);
		List<T> result = new ArrayList<T>();
		SortUtils.popHeap(heapList, result, orderFunc, 2);
		return result;
	}

	// build heap
	private static <T> List<T> buildHeap(List<T> data, Order<T> orderFunc, int latitude) {
		List<T> result = new ArrayList<T>();
		if (data.size() > 0) {
			result.add(data.get(0));
			for (int i = 1; i < data.size(); i++) {
				result.add(data.get(i));
				SortUtils.goUp(result, orderFunc, i, latitude);
			}
		}
		return result;
	}

	// UP
	private static <T> void goUp(List<T> heapList, Order<T> orderFunc, int i, int latitude) {
		while (i > 0) { // top end
			int pid = (i + 1) / 2 - 1;
			// order
			if (orderFunc.isAInFrontOfB(heapList.get(i), heapList.get(pid))) {
				// exchange
				SortUtils.exchange(heapList, pid, i);
				// need to go up
				i = pid;
				continue;
			}
			// no need to go up
			break;
		}
	}

	// DOWN
	private static <T> void goDown(List<T> heapList, Order<T> orderFunc, int pid, int latitude) {
		while (pid < heapList.size()) { // no where go down
			int caid = (pid + 1) * 2 - 1;
			int cbid = (pid + 1) * 2;
			if (caid < heapList.size()) {// a exist
				if (cbid < heapList.size() // b exist
						&& orderFunc.isAInFrontOfB(heapList.get(cbid), heapList.get(caid)) // b rule a
						&& orderFunc.isAInFrontOfB(heapList.get(cbid), heapList.get(pid)) // b rule p
				) {
					SortUtils.exchange(heapList, pid, cbid);
					// need to go down
					pid = cbid;
					continue;
				}
				if (orderFunc.isAInFrontOfB(heapList.get(caid), heapList.get(pid))) { // a rule p
					SortUtils.exchange(heapList, pid, caid);
					// need to go down
					pid = caid;
					continue;
				}
			}
			// no need to go down
			break;
		}
	}

	// POP
	private static <T> void popHeap(List<T> heapList, List<T> result, Order<T> orderFunc, int latitude) {
		while (heapList.size() > 0) {
			// pop head
			result.add(heapList.get(0));
			// exchange end to head
			heapList.set(0, heapList.get(heapList.size() - 1));
			heapList.remove(heapList.size() - 1);
			// head go down
			SortUtils.goDown(heapList, orderFunc, 0, latitude);
		}
	}

	// exchange list object
	private static <T> void exchange(List<T> heapList, int a, int b) {
		T t = heapList.get(a);
		heapList.set(a, heapList.get(b));
		heapList.set(b, t);
	}

}


猜你喜欢

转载自blog.csdn.net/devil_129/article/details/78395132
今日推荐