Java直接插入排序算法实现

 更新时间:2014年01月03日 15:09:01   作者:  
这篇文章主要介绍了Java直接插入排序算法实现,有需要的朋友可以参考一下

序:一个爱上Java最初的想法一直没有磨灭:”分享我的学习成果,不管后期技术有多深,打好基础很重要“。

工具类Swapper,后期算法会使用这个工具类:

复制代码 代码如下:

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

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

相关文章

  • quarzt定时调度任务解析

    quarzt定时调度任务解析

    这篇文章主要介绍了quarzt定时调度任务,具有一定参考价值,需要的朋友可以了解下。
    2017-12-12
  • spring mvc rest 接口选择性加密解密详情

    spring mvc rest 接口选择性加密解密详情

    这篇文章主要介绍了spring mvc rest 接口选择性加密解密详情,spring mvc rest接口以前是采用https加密的,但是现在需要更加安全的加密。而且不是对所有的接口进行加密,是对部分接口进行加密,接口返回值进行解密
    2022-07-07
  • Java实现基于NIO的多线程Web服务器实例

    Java实现基于NIO的多线程Web服务器实例

    在本篇文章里小编给大家整理的是关于Java实现基于NIO的多线程Web服务器实例内容,需要的朋友们可以学习下。
    2020-03-03
  • Spring boot将配置属性注入到bean类中

    Spring boot将配置属性注入到bean类中

    本篇文章主要介绍了Spring boot将配置属性注入到bean类中,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • Java中controller层如何接收带参数的查询

    Java中controller层如何接收带参数的查询

    本文主要介绍了Java中controller层如何接收带参数的查询,在控制器层接收带参数的查询可以通过多种方式实现,下面就详细的介绍一下,感兴趣的可以了解一下
    2023-08-08
  • spring.mvc.servlet.load-on-startup属性方法源码解读

    spring.mvc.servlet.load-on-startup属性方法源码解读

    这篇文章主要介绍了spring.mvc.servlet.load-on-startup的属性方法源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • SpringCloud 搭建企业级开发框架之实现多租户多平台短信通知服务(微服务实战)

    SpringCloud 搭建企业级开发框架之实现多租户多平台短信通知服务(微服务实战)

    这篇文章主要介绍了SpringCloud 搭建企业级开发框架之实现多租户多平台短信通知服务,系统可以支持多家云平台提供的短信服务。这里以阿里云和腾讯云为例,集成短信通知服务,需要的朋友可以参考下
    2021-11-11
  • maven中下载jar包源码和javadoc的命令介绍

    maven中下载jar包源码和javadoc的命令介绍

    这篇文章主要介绍了maven中下载jar包源码和javadoc的命令介绍,本文讲解了Maven命令下载源码和javadocs、通过配置文件添加、配置eclipse等内容,需要的朋友可以参考下
    2015-03-03
  • Spring Boot利用Thymeleaf发送Email的方法教程

    Spring Boot利用Thymeleaf发送Email的方法教程

    spring Boot默认就是使用thymeleaf模板引擎的,下面这篇文章主要给大家介绍了关于在Spring Boot中利用Thymeleaf发送Email的方法教程,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-08-08
  • 如何使用Spring Validation优雅地校验参数

    如何使用Spring Validation优雅地校验参数

    这篇文章主要介绍了如何使用Spring Validation优雅地校验参数,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07

最新评论