In the following chapter, we talk about the principle and code of bubble sorting, and today we talk about the logic of inserting algorithms.
Unlike bubbling sorting, the sorting algorithm is to select an element to compare in order with the element that precedes it. For example, I choose the first element, I want to determine the size of the i-1 element.
The insertion sort is also divided into two loops, the jacket loop such as a pointer, to choose from the first few elements to compare, and the inner loop to start comparing the selection of elements and the size of the preceding elements, to sort.
The code is as follows:
Package Cn.tgb.sort;import java.util.arrays;//Insert Sort public class Insertsort {public static void main (string[] args) {//Generate with Number of machines int[] values = new int[] {(int) (Math.random () * +), (int) (Math.random () *), (int) (Math.random () * +), (int) (M Ath.random () * +), (int) (Math.random () * +), (int) (Math.random () * +), (int) (Math.random () * 100)};//print out random number Syste M.out.println (values);//start sorting from the second element of the array, because the first element itself must be an ordered for (int i = 0; i < values.length; i++) {//Won Take the I-element int key = values[i];//for every I-loop to get the ordinal number of the i-1 int j = i-1;//If it is an element then do not enter the loop///And compare with the previous element, if found to be smaller than the previous primitive, then swap the position, and finally complete the sort. while (J >= 0 && Values[j] > key) {values[j + 1] = values[j];values[j] = key;j--;} Print out the sort results for each time System.out.println ("+ (i + 1) +" Second Order "), for (int k = 0; k < values.length; k++) {System.out.print (values [K] + "");} System.out.println ("");} Print an array//finally print out the sorted random number System.out.println (values) (arrays.tostring);}}
Execution Result:
You can see the result of each loop sort, and the difference between the bubbling sort, the previous loops, and the order of the elements, after the last few loops, to begin sorting.
There is an ordered sequence of data that requires a number to be inserted into the sorted data sequence, but this data sequence is still ordered after insertion , This is the time to use a the new
sorting Method--inserting sort, the basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, In order to get a new, sequential data with a number plus one,
The algorithm is suitable for ordering small amounts of data, and the time complexity is O (n^2). is a stable sorting method.
Finally, comparing bubbles and insertions, in the best case, the bubble sort requires O (n^2) exchange, while the insertion sort is as long as O (n) is exchanged. The implementation of the bubbling sort (similar to the following)
Usually the ordered sequence is poorly run (O (n^2)), and the insertion order in this example requires only O (n) operations. So many modern algorithmic textbooks avoid the use of bubble sorting,
Instead, replace it with an insert sort. Bubble sort if the internal loop can be run for the first time, a flag is used to indicate the possibility of swapping, or the best complexity can be reduced to O (n).
In this case, there is no need for a sorted sequence to be exchanged. If the order of visits is reversed in each visit, the efficiency can be improved slightly. Sometimes called cocktail sort,
Because the algorithm will shuttle from one end of the sequence to the other.
Java re-learning-algorithm insertion sort