Java选择排序算法(二)

2014-11-24 07:20:30 · 作者: · 浏览: 1
f has exchange then swap the two elements Swapper.swap(i, min, array); } } } } /** * Sort the array of generic T with selection sort order * * @param array * the array of generic */ public > 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.csdn.net/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://blog.csdn.net/ysjian_pingcx/article/details/8656048 选择排序算法源码免积分下载:http://download.csdn.net/detail/ysjian_pingcx/6794271