First, the idea of sequencing
Look at the original array as two pieces, a piece is already ordered (we call the order area), a piece is to be sorted (we call the unordered zone), and constantly take its first element from the disordered area, search for the element should be placed in the order of where the area, and put the element into that position, complete this step, the order of the length 1, the unordered zone length-1, until the unordered zone length is 0, that is, the unordered area no longer have elements, the order is complete.
Second, the selection of ordered and disordered areas
The first element of the input array is defined as an ordered area (only one element, how it is placed in order), and the remaining elements are defined in the unordered area.
Third, search for the right location
It is not difficult to find that the last element of an element must be greater than or equal to the first element of an array, except for the top and tail of an ordinal, and that element must be less than or equal to the element. From this idea can be refined to find the right place to store the insertion element method, we can from the last element of the ordered area, that is, the largest ordered element, and the insertion element is compared, if the insertion element is smaller than the element, then the insertion position of the inserted element must precede the last element, The insertion element is then compared to the second-to-last element of the ordered area, and if the insertion element is smaller than the second-to-last element, the insertion element is inserted in the position before the second-to-last element ... And so on, when the inserted element is smaller than the element compared to it, stop comparing, and the position of the inserted element is after this element.
Iv. inserting elements
After finding the right position, we move the position in the ordered area and the element after that position to the right one bit, so that the position is empty, and then inserted into the element.
Five, graphic description
The text is more abstract, we use the graph to illustrate the process of inserting the sort directly, and the following sorts the array {8,3,2,7,4,5}:
First, the ordered and unordered areas are determined, since an element can be considered orderly in any case, then we will use the first element as an ordered area, and the remainder as an unordered area.
Then, we start the insert operation, each insertion is the first element of the selection of unordered areas, such as determining the orderly and unordered areas, you can determine the element to be inserted, that is, the second element of the original array, now insert elements into the ordered area, and then search for the appropriate location
You can find that the inserted element "3" is smaller than the last element "8" in the ordered area, therefore, the element "3" is placed before the element "8", after the end of the comparison, the element "3" before the element, can be considered as element "3" before the element is smaller than the end comparison, the element "8" to move to the right one bit, and the element "3" Insert the search to the position, the first round to insert the end, is not feeling very simple? Let's keep looking down.
Then take out the first element of the unordered zone "2", and put it in the ordered area with the order of the elements of the area to compare, the left side of the midline is ordered, the right side of the unordered area, because of the given, the following figure is not clearly explained!!
It is important to note that inserting elements in the Insert sort search for the right position does not come across an element that is smaller than the insert element, but instead records the location and updates the record in the next comparison.
Obviously, the element "2" is smaller than the element "8", so the insertion position is temporarily stuck between the element "3" and the element "8", and recorded, the element "2" continues to compare, the element "2" is smaller than the element "3", and the Element "3" is no longer the element, so the element "2" insertion position Before, the element "3" and the element "8" are shifted to the right one bit, the element "2" is inserted into the search location, the second end of the insertion.
The next few steps are similar and not listed, let's take a look at how Java code is implemented.
VI. implementation of the Code
1 /**2 * method for direct insertion of sorting3 * @paraman array that needs to be sorted4 */5 Public voidInsertsort (int[] a) {6 //iterates over all elements except the first element, inserting an ordered area of element one by one in the initial unordered region7 for(inti = 1; i < a.length; i++) {8 //Storing insert elements9 inttemp =A[i];Ten //define J Variables and initialize, j variables for insertion position positioning One intj = 0; A //Search for insertion position - for(j = i-1; J >= 0; j--) { - //starting from the last element in the ordered area, if the comparison element is larger than the element being inserted, move to the right one the if(Temp <A[j]) { -A[j + 1] =A[j]; - } - //If the comparison element is smaller than the insert element, the end comparison + if(Temp >A[j]) { - Break; + } A } at //inserting inserted elements into the appropriate location to search -A[j + 1] =temp; - //View each step of the insert situation - System.out.println (Arrays.tostring (a)); - } - }
Sorts the array {8, 3, 2, 7, 4, 5}, with each step being inserted as follows:
The final row is the end result of the order.
This article is original, reprint please specify!
An algorithm that likes to jump in queue: Direct Insert sort (Java implementation)