Insertion Sort [Data Structures] (Review)

  It is mainly composed of three important algorithms of insertion sort: direct insertion sort, half-insertion sort and Hill sort.

  The basic idea is to talk about a record to be sorted at a time, and insert it into the previously sorted subsequence according to its key size until all records are inserted.

  Insertion sort is    stable O(n^2)


  Applicability: The direct insertion sort algorithm is suitable for linear tables of sequential storage and linked storage. When chained storage, you can find the position of the specified element from front to back

  Most sorting algorithms only work with linear lists that are stored sequentially.

1     /** 
2       * insertion sort
 3       * average O(n^2), best O(n), worst O(n^2); space complexity O(1); stable; simple
 4       * @author zuo
 5       *
 6       */ 
7      static  void insertionSort( int [] a) {
 8          int tmp;
 9          for ( int i=1;i<a.length;i++ ){
 10              for ( int j=i;j>0;j -- ){
 11                  if (a[j]<a[j-1 ]){
 12                      tmp=a[j-1 ];
 13                     a[j-1]=a[j];
14                     a[j]=tmp;
15                 }
16             }
17         }
18     }
19     
20     public static void main(String[] args) {
21         int array[] = {10,9,2,3,6,4,7,1,5,11,8};
22         insertionSort(array);
23         for (int i : array)
24             System.out.print(i + " ");
25     }

Initial: 10, 9, 2, 3, 6, 4, 7, 1, 5, 11, 8

First trip: 9, 10, 2, 3, 6, 4, 7, 1, 5, 11, 8

Second trip: 2, 9, 10, 3, 6, 4, 7, 1, 5, 11 , 8

Third trip: 2, 3, 9, 10, 6, 4, 7, 1, 5 , 11, 8

Fourth trip: 2, 3, 6, 9, 10, 4, 7, 1, 5 , 11, 8

Fifth trip: 2, 3, 4, 6, 9, 10, 7, 1, 5, 11, 8

6th trip: 2, 3, 4, 6, 7, 9, 10, 1, 5, 11, 8

Seventh trip: 1, 2, 3, 4, 6, 7, 9, 10, 5, 11, 8

Eighth trip: 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 8

Ninth: 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 8

Tenth time: 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

 

half-insertion sort


 

  ①For each insertion, find the position where the element to be inserted should be inserted from the previous ordered sub-table; 

  ② Make room for the insertion position and copy the element to be inserted to the insertion position in the table.   Note that in this algorithm, elements are always moved while comparing, and the comparison and moving operations will be separated below, that is, first fold in half to find the position to be inserted, and then move all elements after the position to be inserted in agreement. When the sorting table is a linear table with sequential storage, the direct insertion sorting algorithm can be improved as follows: Since it is a linear table with sequential storage, the search for the ordered sub-table can be realized by half-searching. After determining the position to be inserted, the elements can be moved backward uniformly. 

1     /** 
2       * Insertion sort 
 by half 3       * Search by half only reduces the number of comparisons, but the number of element moves remains the same.
4       * Its space complexity O(1), time complexity O(n^2), it is a stable sorting algorithm
 5       * @param data
 6       * @author zuo
 7       */ 
8      static  void binaryInsertSort( int [] data) {  
 9          for ( int i=1;i<data.length;i++ ){
 10              if (data[i]<data[i-1 ]){
 11                  int tmp=data[i]; // in the cache element value 
12                  int low=0; //Record the left edge of the search range 
13                  int high=i-1; // record the right edge of the search range 
14                  while (low<= high){
 15                      int mid=(low+high)/2; // record the middle 
16                      if ( data[mid]<tmp){ // Compare the middle position data and the data size to narrow the search range 
17                          low=mid+1 ;
 18                      } else {
 19                          high=mid-1 ;
 20                      }
 21                  }
 22                  for ( int j =i;j>low;j-- ){
 23                     data[j]=data[j-1];
24                 }
25                 data[low]=tmp;
26                 print(data);
27             }
28         }
29     }  
30     
31     public static void main(String[] args) {
32         int array[] = {10,9,2,3,6,4,7,1,5,11,8};
33         binaryInsertSort(array);
34         for (int i : array)
35             System.out.print(i + " ");
36     }

 

Each pass is the same as direct insertion sort.

Performance Analysis:

The halved search only reduces the number of comparisons, but the number of element moves remains the same. About O(nlog2n), the number of comparisons has nothing to do with the initial state of the table to be sorted, but only depends on the number of elements n in the table;

The number of changes of elements does not change, it depends on the initial state of the table to be sorted, so the time complexity of half-insertion sorting is still O(n^2).

 

Hill sort

 


 

 Also known as shrinking incremental sort.

The basic idea of ​​Hill sorting is: first, divide the list to be sorted into several "special" word lists in the shape of L[i,i+d,i+2d,...,i+kd], and insert them directly. sort;

Then take the second step size d2<d1, repeat the above process until the obtained dt=1, that is, all records have been placed in the same group, and then perform direct insertion sorting,

Since there is already good local order at this time, the final result can be obtained quickly. 

 

 

 

 

 

 

 

 

 

Performance Analysis:

Space efficiency: only a constant number of auxiliary units are used, and the space complexity is 0(1)

Time efficiency: Since the time complexity of Hill sorting depends on the function of the incremental sequence, which involves a mathematically unsolved problem, its time complexity analysis is difficult. When n is in a certain range, the world complexity of Hill sort is about O(n^1.3). The time complexity of Hill sort is O(n^2) in the worst case.

Stability: When records with the same keyword are divided into different word lists, the relative order between them may change. Therefore, Hill sorting is an unstable sorting method.

For example: table L={3, 2 , 2}, after one sorting, L={2, 2 , 3}, obviously the relative order of 2 and 2 has changed.

Applicability: The Hill sorting algorithm is only applicable when the linear table is stored sequentially.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326561397&siteId=291194637