The implementation of Java Direct insertion sort algorithm _java

Source: Internet
Author: User
Tags comparable

Preface: A Fall in love with the original idea of Java has not been erased: "To share My learning results, no matter how deep the late technology, lay a good foundation is very important."

Tool Class Swapper, the later algorithm uses this tool class:

Copy Code code as follows:

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 is 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 is 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 + "]");
}
}
}

Direct Insert Sort, Insertionsortord:

Copy Code code as follows:

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 am 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;
}
}
}
}
}

Test Testinsertionsortord:

Copy Code code as follows:

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));
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.