最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java直接插入排序算法實現(xiàn)

 更新時間:2014年01月03日 15:09:01   作者:  
這篇文章主要介紹了Java直接插入排序算法實現(xiàn),有需要的朋友可以參考一下

序:一個愛上Java最初的想法一直沒有磨滅:”分享我的學(xué)習(xí)成果,不管后期技術(shù)有多深,打好基礎(chǔ)很重要“。

工具類Swapper,后期算法會使用這個工具類:

復(fù)制代碼 代碼如下:

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:

復(fù)制代碼 代碼如下:

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

測試TestInsertionSortord:

復(fù)制代碼 代碼如下:

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)文章

  • quarzt定時調(diào)度任務(wù)解析

    quarzt定時調(diào)度任務(wù)解析

    這篇文章主要介紹了quarzt定時調(diào)度任務(wù),具有一定參考價值,需要的朋友可以了解下。
    2017-12-12
  • spring mvc rest 接口選擇性加密解密詳情

    spring mvc rest 接口選擇性加密解密詳情

    這篇文章主要介紹了spring mvc rest 接口選擇性加密解密詳情,spring mvc rest接口以前是采用https加密的,但是現(xiàn)在需要更加安全的加密。而且不是對所有的接口進(jìn)行加密,是對部分接口進(jìn)行加密,接口返回值進(jìn)行解密
    2022-07-07
  • Java實現(xiàn)基于NIO的多線程Web服務(wù)器實例

    Java實現(xiàn)基于NIO的多線程Web服務(wù)器實例

    在本篇文章里小編給大家整理的是關(guān)于Java實現(xiàn)基于NIO的多線程Web服務(wù)器實例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • Spring boot將配置屬性注入到bean類中

    Spring boot將配置屬性注入到bean類中

    本篇文章主要介紹了Spring boot將配置屬性注入到bean類中,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Java中controller層如何接收帶參數(shù)的查詢

    Java中controller層如何接收帶參數(shù)的查詢

    本文主要介紹了Java中controller層如何接收帶參數(shù)的查詢,在控制器層接收帶參數(shù)的查詢可以通過多種方式實現(xiàn),下面就詳細(xì)的介紹一下,感興趣的可以了解一下
    2023-08-08
  • spring.mvc.servlet.load-on-startup屬性方法源碼解讀

    spring.mvc.servlet.load-on-startup屬性方法源碼解讀

    這篇文章主要介紹了spring.mvc.servlet.load-on-startup的屬性方法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • SpringCloud 搭建企業(yè)級開發(fā)框架之實現(xiàn)多租戶多平臺短信通知服務(wù)(微服務(wù)實戰(zhàn))

    SpringCloud 搭建企業(yè)級開發(fā)框架之實現(xiàn)多租戶多平臺短信通知服務(wù)(微服務(wù)實戰(zhàn))

    這篇文章主要介紹了SpringCloud 搭建企業(yè)級開發(fā)框架之實現(xiàn)多租戶多平臺短信通知服務(wù),系統(tǒng)可以支持多家云平臺提供的短信服務(wù)。這里以阿里云和騰訊云為例,集成短信通知服務(wù),需要的朋友可以參考下
    2021-11-11
  • maven中下載jar包源碼和javadoc的命令介紹

    maven中下載jar包源碼和javadoc的命令介紹

    這篇文章主要介紹了maven中下載jar包源碼和javadoc的命令介紹,本文講解了Maven命令下載源碼和javadocs、通過配置文件添加、配置eclipse等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • Spring Boot利用Thymeleaf發(fā)送Email的方法教程

    Spring Boot利用Thymeleaf發(fā)送Email的方法教程

    spring Boot默認(rèn)就是使用thymeleaf模板引擎的,下面這篇文章主要給大家介紹了關(guān)于在Spring Boot中利用Thymeleaf發(fā)送Email的方法教程,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-08-08
  • 如何使用Spring Validation優(yōu)雅地校驗參數(shù)

    如何使用Spring Validation優(yōu)雅地校驗參數(shù)

    這篇文章主要介紹了如何使用Spring Validation優(yōu)雅地校驗參數(shù),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07

最新評論

闽侯县| 阿城市| 唐山市| 金寨县| 郧西县| 唐海县| 介休市| 瑞丽市| 大埔区| 北川| 乐昌市| 聂荣县| 桑植县| 福建省| 广南县| 出国| 鄂托克前旗| 紫云| 福海县| 左权县| 扬州市| 尤溪县| 泸州市| 麦盖提县| 沁源县| 耒阳市| 井冈山市| 弋阳县| 侯马市| 永和县| 塔城市| 石棉县| 金坛市| 永川市| 宜阳县| 太原市| 兴仁县| 岑巩县| 仁化县| 林周县| 鞍山市|