欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

工具類Swapper,后期算法會(huì)使用這個(gè)工具類:

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

測(cè)試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)文章

  • Java中的?stop?the?world是什么呢

    Java中的?stop?the?world是什么呢

    這篇文章主要介紹了Java中的stop?the?world是什么呢,從字面上講,就是停止這個(gè)世界,看到這個(gè)字眼,就覺(jué)得這是可怕的事情,那到底什么是stop-the-world,本文給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • springboot集成redis實(shí)現(xiàn)簡(jiǎn)單秒殺系統(tǒng)

    springboot集成redis實(shí)現(xiàn)簡(jiǎn)單秒殺系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了springboot集成redis實(shí)現(xiàn)簡(jiǎn)單秒殺系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 看完這篇文章獲得一些java if優(yōu)化技巧

    看完這篇文章獲得一些java if優(yōu)化技巧

    if 是每個(gè)語(yǔ)言都有的語(yǔ)法,也是最基礎(chǔ)的語(yǔ)法。因?yàn)榇a本來(lái)就很晦澀,所以才有了程序員這個(gè)中間件,今天就聊一下我的一些關(guān)于 if 思路和總結(jié)
    2021-07-07
  • SpringBoot任意版本集成Swagger各種版本的操作指南

    SpringBoot任意版本集成Swagger各種版本的操作指南

    在學(xué)習(xí)Swagger生成API文檔的時(shí)候經(jīng)常會(huì)遇到問(wèn)題,而目前市面上大部分技術(shù)分享者的SpringBoot版本并沒(méi)和我們的同步,導(dǎo)致一些一模一樣的代碼,在我們的項(xiàng)目上卻無(wú)法使用,這是一個(gè)經(jīng)常性的問(wèn)題,本文章就旨在和大家搞定SpringBoot任意版本集成Swagger各種版本
    2024-07-07
  • springboot利用redis、Redisson處理并發(fā)問(wèn)題的操作

    springboot利用redis、Redisson處理并發(fā)問(wèn)題的操作

    這篇文章主要介紹了springboot利用redis、Redisson處理并發(fā)問(wèn)題的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • @RereshScope刷新的原理詳解

    @RereshScope刷新的原理詳解

    在配合配置中心修改配置讓應(yīng)用自動(dòng)刷新配置時(shí),我們要在需要感知配置變化的bean上面加上@RereshScope。如果我們不加上這注解,那么有可能無(wú)法完成配置自動(dòng)刷新。本文就來(lái)和大家講講@RereshScope刷新的原理,需要的可以參考一下
    2022-12-12
  • SpringBoot+Apache tika實(shí)現(xiàn)文檔內(nèi)容解析的示例詳解

    SpringBoot+Apache tika實(shí)現(xiàn)文檔內(nèi)容解析的示例詳解

    Apache tika是Apache開源的一個(gè)文檔解析工具,本文主要為大家介紹了如何在springboot中引入tika的方式解析文檔,感興趣的小伙伴可以了解一下
    2023-07-07
  • SpringBoot整合日志功能(slf4j+logback)詳解(最新推薦)

    SpringBoot整合日志功能(slf4j+logback)詳解(最新推薦)

    Spring使用commons-logging作為內(nèi)部日志,但底層日志實(shí)現(xiàn)是開放的,可對(duì)接其他日志框架,這篇文章主要介紹了SpringBoot整合日志功能(slf4j+logback)詳解,需要的朋友可以參考下
    2024-08-08
  • Java中的內(nèi)部類使用詳情

    Java中的內(nèi)部類使用詳情

    說(shuō)起內(nèi)部類這個(gè)詞,想必很多人都不陌生,但是又會(huì)覺(jué)得不熟悉。原因是平時(shí)編寫代碼時(shí)可能用到的場(chǎng)景不多,用得最多的是在有事件監(jiān)聽的情況下,并且即使用到也很少去總結(jié)內(nèi)部類的用法。今天我們就來(lái)一探究竟
    2022-03-03
  • springboot配置加密的正確姿勢(shì)分享

    springboot配置加密的正確姿勢(shì)分享

    在Spring boot開發(fā)中,需要在application.yml文件里配置數(shù)據(jù)庫(kù)的連接信息,或者在啟動(dòng)時(shí)傳入數(shù)據(jù)庫(kù)密碼,如果不加密,傳明文,數(shù)據(jù)庫(kù)就直接暴露了,下面這篇文章主要給大家介紹了關(guān)于springboot配置加密的正確姿勢(shì),需要的朋友可以參考下
    2022-11-11

最新評(píng)論