JAVA数据结构--插入排序

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

算法描述

一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:

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

如果比较操作的代价比交换操作大的话,可以采用二分查找法来减少比较操作的数目。该算法可以认为是插入排序的一个变种,称为二分查找插入排序

            

  时间复杂度:O(n^2)

  最优时间复杂度:O(n)


Weiss书算法实现

 1 package DataStructures;
 2 
 3 import java.util.Arrays;
 4 
 5 public class InsertionSort {
 6     public static void main(String[] args) {
 7         Integer nums[]=new Integer[]{45,1,22,15,6,3,7,5,14};
 8         insertionsort(nums);
 9         System.out.println("Last is: "+Arrays.toString(nums));
10     }
11     public static <AnyType extends Comparable<? super AnyType>>
12     void insertionsort(AnyType[] a){
13         int j;
14         for(int p=1;p<a.length;p++){
15             AnyType tmp=a[p];
16             for(j=p;j>0&&tmp.compareTo(a[j-1])<0;j--)    //逐一递减对比
17                 a[j]=a[j-1];    //后移一项
18             a[j]=tmp;    //插入替换
19             System.out.println(p+"th  is: "+Arrays.toString(a));
20         }
21     }
22 }

output:

1th  is: [1, 45, 22, 15, 6, 3, 7, 5, 14]
2th  is: [1, 22, 45, 15, 6, 3, 7, 5, 14]
3th  is: [1, 15, 22, 45, 6, 3, 7, 5, 14]
4th  is: [1, 6, 15, 22, 45, 3, 7, 5, 14]
5th  is: [1, 3, 6, 15, 22, 45, 7, 5, 14]
6th  is: [1, 3, 6, 7, 15, 22, 45, 5, 14]
7th  is: [1, 3, 5, 6, 7, 15, 22, 45, 14]
8th  is: [1, 3, 5, 6, 7, 14, 15, 22, 45]
Last is: [1, 3, 5, 6, 7, 14, 15, 22, 45]

算法实现(JAVA)

 1 public class InsertionSort {
 2     public static void main(String[] args){
 3         int []elem={3,4,12,6,87,1,22,45,5};
 4         InsertionSort insertionSort=new InsertionSort();
 5         insertionSort.sort(elem);
 6         for(int i=0;i<elem.length;i++)
 7             System.out.print(elem[i]+" ");
 8     }
 9     public int[] sort(int[] elem){
10         for(int i=1;i<elem.length;i++){
11             for(int j=i-1;j>=0;j--){
12                 if(elem[j]>elem[i]){
13                     int temp=elem[j];
14                     elem[j]=elem[i];
15                     elem[i]=temp;
16                     i--;
17                 }
18             }
19         }
20         return elem;
21     }
22 }
原文地址:https://www.cnblogs.com/xpang0/p/6566377.html