Insert sort [data structure] (review)

Source: Internet
Author: User

Important algorithms that are primarily sorted by three insertions: Direct insert sort, binary insert sort, and hill sort.

The basic idea is that each time a record is to be sorted, it is inserted into a sequence of sub-sequences that are already ordered by its key size until all records are inserted.

  Direct Insert Sort stable O (n^2)

Applicability: the direct insertion sorting algorithm is suitable for linear tables of sequential and chained storage. When chained storage, you can find the location of the specified element from the go

Most sorting algorithms apply only to linear tables that are stored sequentially.

1    /**2 * Insert Sort3 * Average O (n^2), preferably O (n), worst O (n^2), Space complexity O (1), stable, simple4      * @authorZuo5      *6      */7     Static voidInsertionsort (int[] a) {8         inttmp;9          for(inti=1;i<a.length;i++){Ten              for(intj=i;j>0;j--){ One                 if(a[j]<a[j-1]){ ATmp=a[j-1]; -a[j-1]=A[j]; -a[j]=tmp; the                 } -             } -         } -     } +      -      Public Static voidMain (string[] args) { +         intArray[] = {10,9,2,3,6,4,7,1,5,11,8}; A Insertionsort (array); at          for(intI:array) -System.out.print (i + "")); -}

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, ten, 3, 6, 4, 7, 1, 5, 11, 8

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

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

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

Six trips: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

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

Tenth trip:1, 2, 3, 4, 5, 6, 7, 8, 9, 10 , one

Binary Insert Sort

Each time the ① is inserted, it finds the position where the inserted element should be inserted from the preceding ordered sub -table;

② make room for the insertion position and copy the element to be inserted into the table at the insertion point.
Notice that in this algorithm, the element is always moved by the edge comparison, and the comparison is separated from the move operation, that is, binary finds the element's position to be inserted, and then agrees to move all elements after the position to be inserted. When the sort table is a linear table stored sequentially, the direct insertion sorting algorithm can be improved as follows: Because it is a linear table stored sequentially, it is possible to find an ordered child table with binary lookup. Once you have determined where you want to insert, you can move the elements backwards and forwards uniformly.

1    /**2 * Binary Insert sort3 * Binary lookup only reduces the number of comparisons, but the number of moving elements is constant. 4 * Its spatial complexity O (1), Time complexity O (n^2), is a stable sorting algorithm5      * @paramData6      * @authorZuo7      */8     Static voidBinaryinsertsort (int[] data) {  9          for(inti=1;i<data.length;i++){Ten             if(data[i]<data[i-1]){ One                 intTmp=data[i];//the element value at the cache A                 intlow=0;//record the left edge of the search scope -                 intHigh=i-1;//record the right boundary of the search scope -                  while(low<=High ) { the                     intMid= (Low+high)/2;//Record Middle -                     if(data[mid]<tmp) {//Compare intermediate position data and data size to narrow the search -Low=mid+1; -}Else{ +High=mid-1; -                     } +                 } A                  for(intj=i;j>low;j--){ atData[j]=data[j-1]; -                 } -data[low]=tmp; - print (data); -             } -         } in     }   -      to      Public Static voidMain (string[] args) { +         intArray[] = {10,9,2,3,6,4,7,1,5,11,8}; - Binaryinsertsort (array); the          for(intI:array) *System.out.print (i + "")); $}

Each trip is the same as the direct insert sort.

Performance Analysis:

Binary lookup only reduces the number of comparisons, but the number of moves of the element is constant. Approximately o (nlog2n), which is independent of the initial state of the table to be sorted, depends only on the number of elements in the table n;

While the number of changes to the element does not change, it relies on the initial state of the table to be sorted, so the time complexity of binary insertion sorting is still O (n^2).

Hill sort

Also known as narrowing the incremental sort.

The basic idea of Hill sort is: First, divide the table to be sorted into several "special" characters, such as l[i,i+d,i+2d,..., I+KD], and make the direct insertion sort respectively;

Then take the second step d2<d1, repeat the process until the dt=1 is taken, that is, all the records are placed in the same group, and then the direct insertion of the sort,

Because of the good local order, the final result can be obtained quickly.

Performance Analysis:

Space efficiency: Use only a constant number of auxiliary units, space complexity of 0 (1)

Time efficiency: Because the time complexity of the hill sort depends on the function of the increment sequence, which involves the unsolved problems in mathematics, the time complexity analysis is more difficult. When n is in a certain range, the world complexity of the hill sort is approximately O (n^1.3). in the worst case, the time complexity of hill sorting is O (n^2).

Stability: When a record of the same keyword is divided into a different Word table, the relative order between them may change, so the hill sort is an unstable sort method.

For example: Table L={3,2, 2}, after a trip to sort, l={2,2, 3}, obviously the relative order of 2 and 2 changed.

Applicability: The hill sorting algorithm applies only when the linear table is stored sequentially.

Insert sort [data structure] (review)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.