Direct insertion sort of sorting algorithm

Source: Internet
Author: User

The sorting algorithm is divided into many kinds, in which the insertion sort is the most basic sort algorithm. Insert sort includes direct insert sort, binary insert sort and hill sort, these three sort algorithms are essentially the same, but have different auxiliary storage space and time complexity in the process of actual operation and implementation.

First, direct insertion sort


Basic ideas

Direct insertion ordering is the insertion of an element directly into an ordered table, resulting in a new ordered table with a 1 increase in the number of elements. So when we need to sort a group of numbers, we take the 1th element of this set of numbers as an ordered table, then we insert the 2nd element, we get an ordered table with 2 elements, we insert the 3rd element, and so on, until the last element is inserted into the new ordered table, sorting is done.

When you insert a new element into an ordered table that already has an I element, you can choose to search for the new element from the beginning of the first element, from the back to the next, so that you can move the element back in the search so that the new element is inserted directly (see PSEUDOCODE) when the inserted position is found. It is also possible to switch directly in place when the element being inserted is less than the element compared to it, and then continue searching forward until you jump out of the loop (see code below). The advantage of in-place exchange is that it saves some space and prevents the array from overstepping.


Give me a chestnut.

The number of columns to be sorted is: 22 45 12 47 56 36

First time: (22 45) 12 47 56 36

Second time: (12 22 45) 47 56 36

Third time: (12 22 45 47) 56 36

Third time: (12 22 45 47 56) 36

Fourth time: (12 22 36 45 47 56) Sort done!


Algorithm pseudo-code

Insertionsort (a[0...n-1])//Sort the given array with an insert sort//input: An array of n sortable elements a[0...n-1]//output: An array not in descending order A[0...n-1] for i←1 to N-1 do V←a[i] J←i-1 while J >= 0 and A[j]>v do a[j+1]←a[j]//move element j←j+1 a[j+1 ]←v


Analysis of algorithm complexity

As can be seen from the above pseudo-code, the basic operation of the algorithm is the key value comparison A[j]>v, it is obvious that the number of key value comparisons depends on the specific input. In the worst case, the while loop of the above pseudo-code executes from j=i-1 to j=0 each time, that is, the original array is an array in descending order, so for this type of input, the number of key values to compare is:

C Worst (n) =∑ (i=1...n-1) ∑ (j=0...i-1) 1= (n-1) n/2∈ θ (n^2)

In the best case, the original array has been sorted in ascending order, so each while loop is executed only once, so the array key-value comparison for n elements is n ∈ θ (n), but this situation itself doesn't make much sense because we can't expect such a simple input.

According to the book, for an array of random sequences, the average comparison number of directly inserted orders is half of the descending array, which means:

c (N) = (n-1) n/4 ∈ θ (n^2)

Average performance is one-fold faster than worst-case performance, and performs well when encountering a basic ordered array, leading to a direct-insert sort that is ahead of its main competitor in the Basic Sorting algorithm field-selecting sort and bubbling sort, but that doesn't mean it can replace these algorithms, which I'll review in the next article.


(Java)

Public class insertionsort {    public static void main ( String[] args) {            int []  Array=new int[]{22,65,34,18,56,49,0};          int  temp;        for (int i=1;i<array.length;i++) {                     for (int j = i;j > 0;j--) {                 if (Array[j] < array[j-1]) {                     temp =  array[j-1];                     array[j-1] = array[j];                     array[j] = temp;                 }             }        }         for (Int i:array)             system.out.print ( i+ ",");     } }


Level is limited, I hope you big man to correct me!




This article from "Guardian" blog, reproduced please contact the author!

Direct insertion sort of sorting algorithm

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.