程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Java直接插入排序算法實現

Java直接插入排序算法實現

編輯:JAVA編程入門知識

序:一個愛上Java最初的想法一直沒有磨滅:”分享我的學習成果,不管後期技術有多深,打好基礎很重要“。

工具類Swapper,後期算法會使用這個工具類:
代碼如下:

package com.meritit.sortord.util;

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

直接插入排序,InsertionSortord:
代碼如下:

package com.meritit.sortord.insertion;

/**
 * Insertion 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-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;
    }
   }
  }
 }
}

測試TestInsertionSortord:
代碼如下:

package com.meritit.sortord.insertion;

import java.util.Arrays;

/**
 * Test insertion sort order
 *
 * @author ysjian
 * @version 1.0
 * @email [email protected]
 * @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));
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved