Classical sorting algorithms - insertion sort

Insertion Sort principle

Ordering principle is to insert a number of times each size of the insert according to its previously sorted subsequence. Sequentially repeated until all of the digital insertion.

Illustrates the principle of insertion sort (ascending example)

An array [3,4,1,2], for example, an array in ascending order.

First trip second data from the beginning (the first number they have ordered), a comparison with the previous figures, if less than the foregoing figures will shift before the numbers, and the comparison continues forward until the index is less than 0 so far. As shown below

 

The second number 4, before it is inserted into the ordered sequence (3), since the 4> 3 3 therefore plug directly into the back i.e. without moving. The first trip to the end. I pointers move backward, as shown in FIG.

 

A third number, ordered sequence (3,4) is inserted in front of it, since 1 <4 4 it will move one (number 1 will first be saved, without fear of being overwritten), and then let 1 compared with 3, 1 <3, so after a 3 shift. After traversal sequence, insertion into the number 1 position. Then move the pointer i, as shown in FIG.

 

After three trips comparison, digital array has been in good order. The following specific code facie

. 1  public  static  void Sort ( int [] Array) {
 2          for ( int I =. 1; I <be array.length; I ++ ) {
 . 3              int insertValue = Array [I];
 . 4              int InsertIndex I = -1 ;
 . 5              the while (InsertIndex > = 0 && insertValue < Array [InsertIndex]) {
 . 6                  // be inserted in front of the number less than the number corresponding subscript 
. 7                  Array [InsertIndex +. 1] = Array [InsertIndex];
 . 8                  InsertIndex - ;
 . 9              }
 10              //+. 1 to find the position InsertIndex 
. 11              Array [InsertIndex +. 1] = insertValue;
 12 is          }
 13 is      }

Code Analysis

1) The first for loop is used to determine the number of comparison times, I start from 1 (in front of a number, a sequence number of affirmative ordered) array to the last digit

2) Record the current values ​​to be inserted into the front of the sub-sequence (after shift preventing covering sequence number)

3) while the sub-loop sequence according to the order, continue to traverse the forward sequence when the number is greater than the number to be inserted, until it finds less than the current sequence number, or traversed, the position inserted into the corresponding

time complexity

It is seen from the code, an insertion sort has two cycle, so its time complexity is T (n) = O (n ^ 2)

Test the efficiency of algorithms

The same as the previous sorting algorithm, we still generated an array of 100,000 random numbers, using insertion sort method to sort, to see the execution time. Test code as follows

1 public static void main(String []args){
2         int[] array = new int[100000];
3         for (int i=0; i<100000; i++){
4             array[i] = (int)(Math.random()*800000);
5         }
6         long begin = System.currentTimeMillis();
7         sort(array);
8         System.out.println("总耗时="+(System.currentTimeMillis() - begin));
9     }

Test Results

 

We can see the insertion sort algorithm is faster than selection sort, sort the array 100 000 data takes about one second and more time. Next we will introduce insertion algorithm optimization algorithm based on the - Hill sorting algorithm, sorting efficiency whether it will be higher? We look forward to!

to sum up

Insertion sorting algorithm from the second idea is the number array, an ordered sequence inserted in front of it. Turn iterate, iterate until after all the data, namely the array into order.

 

Guess you like

Origin www.cnblogs.com/menglong1108/p/11669085.html