Java选择排序算法

Java 选择排序算法

选择排序算法源码免积分下载: http://download.youkuaiyun.com/detail/ysjian_pingcx/6794271

序:一个爱上Java最初的想法一直没有磨灭:”分享我的学习成果,不管后期技术有多深,打好基础很重要“。
声明:所有源码经由本人原创,上道者皆似曾相识,另未经过权威机构或人士审核和批准,勿做商业用途,仅供学习分享,若要转载,请注明出处。

工具类Swapper,后期算法会使用这个工具类:
/**
 * One util to swap tow element of Array
 * 
 * @author ysjian
 * @version 1.0
 * @email ysjian_pingcx@126.com
 * @QQ 646633781
 * @telephone 18192235667
 * @csdnBlog http://blog.youkuaiyun.com/ysjian_pingcx
 * @createTime 2013-12-20
 * @copyRight Merit
 */
public class Swapper {

	private Swapper() {

	}

	/**
	 * Swap tow elements of the array
	 * 
	 * @param oneIndex
	 *            one index
	 * @param anotherIndex
	 *            another index
	 * @param array
	 *            the array to be swapped
	 * @exception NullPointerException
	 *                if the array is null
	 */
	public static <T extends Comparable<T>> void swap(int oneIndex,
			int anotherIndex, T[] array) {
		if (array == null) {
			throw new NullPointerException("null value input");
		}
		checkIndexs(oneIndex, anotherIndex, array.length);
		T temp = array[oneIndex];
		array[oneIndex] = array[anotherIndex];
		array[anotherIndex] = temp;
	}

	/**
	 * Swap tow elements of the array
	 * 
	 * @param oneIndex
	 *            one index
	 * @param anotherIndex
	 *            another index
	 * @param array
	 *            the array to be swapped
	 * @exception NullPointerException
	 *                if the array is null
	 */
	public static void swap(int oneIndex, int anotherIndex, int[] array) {
		if (array == null) {
			throw new NullPointerException("null value input");
		}
		checkIndexs(oneIndex, anotherIndex, array.length);
		int temp = array[oneIndex];
		array[oneIndex] = array[anotherIndex];
		array[anotherIndex] = temp;
	}

	/**
	 * Check the index whether it is in the arrange
	 * 
	 * @param oneIndex
	 *            one index
	 * @param anotherIndex
	 *            another index
	 * @param arrayLength
	 *            the length of the Array
	 * @exception IllegalArgumentException
	 *                if the index is out of the range
	 */
	private static void checkIndexs(int oneIndex, int anotherIndex,
			int arrayLength) {
		if (oneIndex < 0 || anotherIndex < 0 || oneIndex >= arrayLength
				|| anotherIndex >= arrayLength) {
			throw new IllegalArgumentException(
					"illegalArguments for tow indexs [" + oneIndex + ","
							+ oneIndex + "]");
		}
	}
}
选择排序算法SelectionSortord:
/**
 * Selection sort order, time complexity is O(n2)
 * 
 * @author ysjian
 * @version 1.0
 * @email ysjian_pingcx@126.com
 * @QQ 646633781
 * @telephone 18192235667
 * @csdnBlog http://blog.youkuaiyun.com/ysjian_pingcx
 * @createTime 2013-12-31
 * @copyRight Merit
 * @since 1.5
 */
public class SelectionSortord {

	private static final SelectionSortord INSTANCE = new SelectionSortord();

	private SelectionSortord() {
	}

	/**
	 * Get the instance of SelectionSortord, only just one instance
	 * 
	 * @return the only instance
	 */
	public static SelectionSortord getInstance() {
		return INSTANCE;
	}

	/**
	 * Sort the array of <code>int</code> with selection sort order
	 * 
	 * @param array
	 *            the array of int
	 */
	public void doSort(int... array) {
		if (array != null && array.length > 0) {
			int length = array.length;

			// a flag judge whether the array is in order
			boolean flag = true;
			for (int i = 0; i < length - 1 && flag; i++) {
				flag = false; // set the flag false

				// set the current index value as minimum in every circulation
				int min = i;
				for (int j = i + 1; j < length; j++) {

					// compare with the value after index i
					if (array[min] > array[j]) {
						flag = true; // if not in order,set flag true
						/*
						 * if the value of index j is lower then exchange the
						 * minimum value index
						 */
						min = j;
					}
				}
				if (flag) {

					// if has exchange then swap the two elements
					Swapper.swap(i, min, array);
				}
			}
		}
	}

	/**
	 * Sort the array of generic <code>T</code> with selection sort order
	 * 
	 * @param array
	 *            the array of generic
	 */
	public <T extends Comparable<T>> void doSortT(T[] array) {
		if (array != null && array.length > 0) {
			int length = array.length;
			boolean flag = true;
			for (int i = 0; i < length - 1 && flag; i++) {
				flag = false;
				int min = i;
				for (int j = i + 1; j < length; j++) {
					if (array[min].compareTo(array[j]) > 0) {
						flag = true;
						min = j;
					}
				}
				if (flag) {
					Swapper.swap(i, min, array);
				}
			}
		}
	}
}


测试TestSelectionSortord:
/**
 * Test the selection sort order
 * 
 * @author ysjian
 * @version 1.0
 * @email ysjian_pingcx@126.com
 * @QQ 646633781
 * @telephone 18192235667
 * @csdnBlog http://blog.youkuaiyun.com/ysjian_pingcx
 * @createTime 2013-12-31
 * @copyRight Merit
 */
public class TestSelectionSortord {

	/**
	 * Test
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		SelectionSortord selectSort = SelectionSortord.getInstance();
		int[] array = { 25, 36, 21, 45, 98, 13 };
		System.out.println(Arrays.toString(array));
		selectSort.doSort(array);
		System.out.println(Arrays.toString(array));
		System.out.println("------------------------");
		Integer[] arrayT = { 25, 36, 21, 45, 98, 13 };
		System.out.println(Arrays.toString(arrayT));
		selectSort.doSortT(arrayT);
		System.out.println(Arrays.toString(arrayT));
	}
}

选择排序算法源码免积分下载: http://download.youkuaiyun.com/detail/ysjian_pingcx/6794271
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值