热门标签 | HotTags
当前位置:  开发笔记 > 人工智能 > 正文

Java直接插入排序算法实现

这篇文章主要介绍了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 > 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 int 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     if (array[i]

     // 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 T with insertion sort order
  *
  * @param array
  *            the array of generic
  */
 public > void doSortT(T[] array) {
  if (array != null && array.length > 0) {
   int length = array.length;
   for (int i = 1; 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));
 }
}


推荐阅读
  • 深入解析JVM垃圾收集器
    本文基于《深入理解Java虚拟机:JVM高级特性与最佳实践》第二版,详细探讨了JVM中不同类型的垃圾收集器及其工作原理。通过介绍各种垃圾收集器的特性和应用场景,帮助读者更好地理解和优化JVM内存管理。 ... [详细]
  • 使用Numpy实现无外部库依赖的双线性插值图像缩放
    本文介绍如何仅使用Numpy库,通过双线性插值方法实现图像的高效缩放,避免了对OpenCV等图像处理库的依赖。文中详细解释了算法原理,并提供了完整的代码示例。 ... [详细]
  • 本文将介绍如何使用 Go 语言编写和运行一个简单的“Hello, World!”程序。内容涵盖开发环境配置、代码结构解析及执行步骤。 ... [详细]
  • 线性Kalman滤波器在多自由度车辆悬架主动控制中的应用研究
    本文探讨了线性Kalman滤波器(LKF)在不同自由度(2、4、7)的车辆悬架系统中进行主动控制的应用。通过详细的仿真分析,展示了LKF在提升悬架性能方面的潜力,并总结了调参过程中的关键要点。 ... [详细]
  • 本文探讨了Hive中内部表和外部表的区别及其在HDFS上的路径映射,详细解释了两者的创建、加载及删除操作,并提供了查看表详细信息的方法。通过对比这两种表类型,帮助读者理解如何更好地管理和保护数据。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • C++实现经典排序算法
    本文详细介绍了七种经典的排序算法及其性能分析。每种算法的平均、最坏和最好情况的时间复杂度、辅助空间需求以及稳定性都被列出,帮助读者全面了解这些排序方法的特点。 ... [详细]
  • 本文介绍如何利用动态规划算法解决经典的0-1背包问题。通过具体实例和代码实现,详细解释了在给定容量的背包中选择若干物品以最大化总价值的过程。 ... [详细]
  • 本文详细探讨了Java中的24种设计模式及其应用,并介绍了七大面向对象设计原则。通过创建型、结构型和行为型模式的分类,帮助开发者更好地理解和应用这些模式,提升代码质量和可维护性。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 深入理解C++中的KMP算法:高效字符串匹配的利器
    本文详细介绍C++中实现KMP算法的方法,探讨其在字符串匹配问题上的优势。通过对比暴力匹配(BF)算法,展示KMP算法如何利用前缀表优化匹配过程,显著提升效率。 ... [详细]
  • 探讨一个显示数字的故障计算器,它支持两种操作:将当前数字乘以2或减去1。本文将详细介绍如何用最少的操作次数将初始值X转换为目标值Y。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 本文探讨如何设计一个安全的加密和验证算法,确保生成的密码具有高随机性和低重复率,并提供相应的验证机制。 ... [详细]
author-avatar
大树沧海凝神的天空
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有