Java 比较排序算法
Posted 袁慎建@ThoughtWorks
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ysjian_pingcx@126.com
* @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 ysjian_pingcx@126.com
* @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 ysjian_pingcx@126.com
* @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
以上是关于Java 比较排序算法的主要内容,如果未能解决你的问题,请参考以下文章