Java 比較排序算法

Java 簡單比較排序算法

簡單比較排序算法詳解:        http://blog.csdn.net/ysjian_pingcx/article/details/8652091
簡單比較排序算法源碼下載:http://download.csdn.net/detail/ysjian_pingcx/6750815

序:一個愛上Java最初的想法一直沒有磨滅:”分享我的學習成果,不管後期技術有多深,打好基礎很重要“。
         
            之前即將畢業那會兒,寫過關於排序算法的幾篇文章,有一些大學裏面學到的常見的算法,主要是因爲在應屆生面試的過程中,一些公司經常會問到學生一些這樣的問題,就想把它們分享出來,目的是希望給剛面臨找工作的Java初學者一些幫助,接下來我會貼出這些算法的源碼,同時歡迎去我的空間免積分下載,同是學習者,現在參加工作了,一直進,也想產出一點,希望給那些Java的愛好者,又在起跑線上的學者一些啓發作用,代碼並非最優的,歡迎同行指正。每一個算法源碼都有詳解,這裏面的源碼經過我後期的一些修改,因爲本人遇到了中文亂碼的問題,所以註釋用的是英文的,別以爲我英文有多好,要跨就誇有道去...

聲明:所有源碼經由本人原創,上道者皆似曾相識,另未經過權威機構或人士審覈和批准,勿做商業用途,僅供學習分享,若要轉載,請註明出處。

工具類Swapper,後期算法會使用這個工具類:
package com.meritit.sortord.comparison;

/**
 * One util to swap tow element of Array
 * 
 * @author ysjian
 * @version 1.0
 * @email [email protected]
 * @QQ 646633781
 * @telephone 18192235667
 * @csdnBlog http://blog.csdn.net/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 + "]");
		}
	}
}
下面是簡單比較排序源碼:
ComparisonSortord:
package com.meritit.dm.csdn.sort.compare;

import com.meritit.dm.csdn.sort.Swapper;

/**
 * Comparison sort order, time complexity is O(n2)
 * 
 * @author ysjian
 * @version 1.0
 * @email [email protected]
 * @QQ 646633781
 * @telephone 18192235667
 * @csdnBlog http://blog.csdn.net/ysjian_pingcx
 * @createTime 2013-12-20
 * @copyRight Merit
 * @since 1.5
 */
public class ComparisonSortord {

	private static final ComparisonSortord INSTANCE = new ComparisonSortord();

	private ComparisonSortord() {
	}

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

	/**
	 * Sort the array of int with comparison sort order
	 * 
	 * @param array
	 *            the array of int
	 */
	public void doSort(int... array) {
		if (array != null && array.length > 0) {
			int length = array.length;
			for (int i = 0; i < length - 1; i++) {
				/*
				 * the current element of index i compares to any other elements
				 * after i
				 */
				for (int j = i + 1; j < length; j++) {

					/*
					 * if the current element of index i is above the element,
					 * then swap them
					 */
					if (array[i] > array[j]) {
						Swapper.swap(i, j, array);
					}
				}
			}
		}
	}

	/**
	 * Sort the array of generic <code>T</code> with comparison 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;
			for (int i = 0; i < length - 1; i++) {
				for (int j = i + 1; j < length; j++) {
					if (array[i].compareTo(array[j]) > 0) {
						Swapper.swap(i, j, array);
					}
				}
			}
		}
	}
}
測試TestComparisonSortord:
package com.meritit.dm.csdn.sort.compare;

import java.util.Arrays;

/**
 * Test the comparison sort order
 * 
 * @author ysjian
 * @version 1.0
 * @email [email protected]
 * @QQ 646633781
 * @telephone 18192235667
 * @csdnBlog http://blog.csdn.net/ysjian_pingcx
 * @createTime 2013-12-21
 * @copyRight Merit
 */
public class TestComparisonSortord {
	
	/**
	 * Test
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		ComparisonSortord compareSort = ComparisonSortord.getInstance();
		int[] array = { 25, 35, 11, 45, 98, 65 };
		System.out.println(Arrays.toString(array));
		compareSort.doSort(array);
		System.out.println(Arrays.toString(array));
		System.out.println("------------------------");
		Integer[] arrays = { 25, 35, 11, 45, 98, 65 };
		System.out.println(Arrays.toString(arrays));
		compareSort.doSortT(arrays);
		System.out.println(Arrays.toString(arrays));
	}
}
簡單比較排序算法詳解:        http://blog.csdn.net/ysjian_pingcx/article/details/8652091
簡單比較排序算法源碼下載:http://download.csdn.net/detail/ysjian_pingcx/6750815



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章