排序总结之插入式排序


算法概述

插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。

插入排序主要分为三种,即直接插入,二分插入(利用二分法来减少比较次数),希尔排序(又称缩小增量排序)

插入的主要步骤:

1,从第一个元素开始,该元素可以认为已经被排序
2,取出下一个元素,在已经排序的元素序列中从后向前扫描
3,如果该元素(已排序)大于新元素,将该元素移到下一位置
4,重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
5,将新元素插入到该位置后
6,重复步骤2~5

复杂度分析:

直接插入复杂度:

最差时间复杂度 O(n^2)
最优时间复杂度 O(n)
平均时间复杂度 O(n^2)
最差空间复杂度 总共O(n) ,需要辅助空间O(1)

二分插入复杂度:

 

 

插入每个记录需要O(log i)比较,最多移动i+1次,最少2次
最佳情况O(nlog n),最差和平均情况O(n^2)。

希尔排序复杂度:

最差时间复杂度 根据步长串行的不同而不同。 已知最好的: O(n\log^2 n)
最优时间复杂度 O(n)
平均时间复杂度 根据步长串行的不同而不同。
最差空间复杂度 O(n)

代码示例:

package com.louxue.sort;

import java.util.Arrays;
/**
 * 插入排序
 * @author louxuezheng 2014年3月10日
 */
public class InsertSort {
 public static void main(String... args) {
  int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
    99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
  // new InsertSort().insertSort(a);// 直接插入排序
  // new InsertSort().shellSort(a);
  // new InsertSort().shellSort2(a);
  new InsertSort().binaryInsertSort(a);
  System.out.println(Arrays.toString(a));
 }

 /**
  * 直接插入排序是稳定的排序
  * 基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排 好顺序的,
  * 现在要把第n个数插到前面的有序数中,使得这n个数 也是排好顺序的。
  * 如此反复循环,直到全部排好顺序。
  * @param a
  */
 private void insertSort(int[] a) {
  int temp = 0;
  for(int i=1;i<a.length;i++){
   int j=i-1;
   temp = a[i];// 要被插入的第i个数,这个数与前面的排好序的数进行比较。比它大的整体都向后移动
   while (j >= 0 && temp < a[j]) {
    a[j + 1] = a[j];
    j--;
   }
   a[j + 1] = temp;
  }
 }

 /**
  * shell排序是不稳定的。
  * 基本思想:算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,
  * 每组中记录的下标相差d. 对每组中全部元素进行直接插入排序,然后再用一个较小的增量
  * (d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。
  *
  * @param a
  */
 private void shellSort(int[] a) {
  double d1 = a.length;
  int temp = 0;
  while (true) {
   d1 = Math.ceil(d1 / 2);// 向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。
   int d = (int) d1;
   for (int x = 0; x < d; x++) {
    for (int i = x + d; i < a.length; i += d) {
     int j = i - d;
     temp = a[i];
     for (; j >= 0 && temp < a[j]; j -= d) {
      a[j + d] = a[j];
     }
     a[j + d] = temp;
    }
   }
   if (d == 1)
    break;
  }
 }

 /**
  * Knuth 提出的d=[d/3]+1,下取整。
  *
  * @param a
  */
 private void shellSort2(int[] a) {
  int d = a.length;
  int temp = 0;
  do {
   d=d/3+1;
   for (int x = 0; x < d; x++) {
    for (int i = x + d; i < a.length; i += d) {
     int j = i - d;
     temp = a[i];
     for (; j >= 0 && temp < a[j]; j -= d) {
      a[j + d] = a[j];
     }
     a[j + d] = temp;
    }
   }
  } while (d > 1);
 }
 /**
  * 折半插入排序是稳定的排序
  * 又称为二分插入排序,基本思想:设在数组中前i-1个元素是已经排好序的,
  * 在插入a[i]时,利用二分搜索的方法找到a[i]的位置插入。
  * @param a
  */
 private void binaryInsertSort(int[] a) {
  int temp = 0;
  int i, low, high, middle, k;
  for (i = 1; i < a.length; i++) {
   temp = a[i];
   low = 0;
   high = i - 1;
   while (low <= high) {
    middle = (low + high) / 2;
    if (temp < a[middle]) {
     high = middle - 1;
    } else
     low = middle + 1;
   }
   for (k = i - 1; k >= low; k--)
    a[k + 1] = a[k];// 成块移动空出插入位置
   a[low] = temp;
  }
 }
}

本文永久更新链接地址:

相关内容