Java直接插入排序算法實(shí)現(xiàn)
序:一個(gè)愛上Java最初的想法一直沒有磨滅:”分享我的學(xué)習(xí)成果,不管后期技術(shù)有多深,打好基礎(chǔ)很重要“。
工具類Swapper,后期算法會(huì)使用這個(gè)工具類:
package com.meritit.sortord.util;
/**
* 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 + "]");
}
}
}
直接插入排序,InsertionSortord:
package com.meritit.sortord.insertion;
/**
* Insertion 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-31
* @copyRight Merit
* @since 1.5
*/
public class InsertionSortord {
private static final InsertionSortord INSTANCE = new InsertionSortord();
private InsertionSortord() {
}
/**
* Get the instance of InsertionSortord, only just one instance
*
* @return the only instance
*/
public static InsertionSortord getInstance() {
return INSTANCE;
}
/**
* Sort the array of <code>int</code> with insertion sort order
*
* @param array
* the array of int
*/
public void doSort(int... array) {
if (array != null && array.length > 0) {
int length = array.length;
// the circulation begin at 1,the value of index 0 is reference
for (int i = 1; i < length; i++) {
if (array[i] < array[i - 1]) {
// if value at index i is lower than the value at index i-1
int vacancy = i; // record the vacancy as i
// set a sentry as the value at index i
int sentry = array[i];
// key circulation ,from index i-1 ,
for (int j = i - 1; j >= 0; j--) {
if (array[j] > sentry) {
/*
* if the current index value exceeds the
* sentry,then move backwards, set record the new
* vacancy as j
*/
array[j + 1] = array[j];
vacancy = j;
}
}
// set the sentry to the new vacancy
array[vacancy] = sentry;
}
}
}
}
/**
* Sort the array of generic <code>T</code> with insertion 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 = 1; i < length; i++) {
if (array[i].compareTo(array[i - 1]) < 0) {
T sentry = array[i];
int vacancy = i;
for (int j = i - 1; j >= 0; j--) {
if (array[j].compareTo(sentry) > 0) {
array[j + 1] = array[j];
vacancy = j;
}
}
array[vacancy] = sentry;
}
}
}
}
}
測(cè)試TestInsertionSortord:
package com.meritit.sortord.insertion;
import java.util.Arrays;
/**
* Test insertion sort order
*
* @author ysjian
* @version 1.0
* @email ysjian_pingcx@126.com
* @QQ 646633781
* @telephone 18192235667
* @createTime 2013-12-31
* @copyRight Merit
*/
public class TestInsertionSortord {
public static void main(String[] args) {
InsertionSortord insertSort = InsertionSortord.getInstance();
int[] array = { 3, 5, 4, 2, 6 };
System.out.println(Arrays.toString(array));
insertSort.doSort(array);
System.out.println(Arrays.toString(array));
System.out.println("---------------");
Integer[] array1 = { 3, 5, 4, 2, 6 };
System.out.println(Arrays.toString(array1));
insertSort.doSortT(array1);
System.out.println(Arrays.toString(array1));
}
}
相關(guān)文章
Java實(shí)現(xiàn)基于NIO的多線程Web服務(wù)器實(shí)例
在本篇文章里小編給大家整理的是關(guān)于Java實(shí)現(xiàn)基于NIO的多線程Web服務(wù)器實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-03-03
Java中controller層如何接收帶參數(shù)的查詢
本文主要介紹了Java中controller層如何接收帶參數(shù)的查詢,在控制器層接收帶參數(shù)的查詢可以通過多種方式實(shí)現(xiàn),下面就詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08
spring.mvc.servlet.load-on-startup屬性方法源碼解讀
這篇文章主要介紹了spring.mvc.servlet.load-on-startup的屬性方法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
SpringCloud 搭建企業(yè)級(jí)開發(fā)框架之實(shí)現(xiàn)多租戶多平臺(tái)短信通知服務(wù)(微服務(wù)實(shí)戰(zhàn))
這篇文章主要介紹了SpringCloud 搭建企業(yè)級(jí)開發(fā)框架之實(shí)現(xiàn)多租戶多平臺(tái)短信通知服務(wù),系統(tǒng)可以支持多家云平臺(tái)提供的短信服務(wù)。這里以阿里云和騰訊云為例,集成短信通知服務(wù),需要的朋友可以參考下2021-11-11
Spring Boot利用Thymeleaf發(fā)送Email的方法教程
spring Boot默認(rèn)就是使用thymeleaf模板引擎的,下面這篇文章主要給大家介紹了關(guān)于在Spring Boot中利用Thymeleaf發(fā)送Email的方法教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-08-08
如何使用Spring Validation優(yōu)雅地校驗(yàn)參數(shù)
這篇文章主要介紹了如何使用Spring Validation優(yōu)雅地校驗(yàn)參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

