数据结构学习之直接插入排序

转自:http://blog.csdn.net/m13666368773/article/details/7506678  

   数据结构学习之直接插入排序

    看这张图不知道大家可否理解了,在插入排序中,数组会被划分为两种,“有序数组块”和“无序数组块”,第一遍的时候从“无序数组块”中提取一个数20作为有序数组块;第二遍的时候从”无序数组块“中提取一个数60有序的放到”有序数组块中“,也就是20,60;第三遍的时候同理,不同的是发现10比有序数组的值都小,因此20,60位置后移,腾出一个位置让10插入,然后按照这种规律就可以全部插入完毕。

 1 package com.swust.插入排序;
 2 
 3 import com.swust.utils.ArrayUtils;
 4 
 5 public class Example1 {
 6     public static void main(String[] args) {
 7         int[] arr = ArrayUtils.createArray(10);
 8         ArrayUtils.showArray(arr);
 9         sort(arr);
10         String string = "";
11         string.intern();
12         System.out.println();
13         ArrayUtils.showArray(arr);
14     }
15     public static void sort(int[] list){
16              //无须序列  
17             for (int i = 1; i < list.length; i++)  
18             {  
19                 int temp = list[i]; //10,11,33,1,3 
20   
21                 int j;  
22                 //若提取的数据大于当前有序数组中的数据,则只需直接插入,无需移动数组中元素
23                 for (j = i - 1; j >= 0 && temp < list[j]; j--)  
24                 {  
25                     list[j + 1] = list[j];  
26                 }  
27                 list[j+1] = temp;  
28             }  
29         }
30 }