Insert sorting: insert sorting
Basic Principles of insert sorting
The insert sort method is closer to life than the bubble sort method and the select sort method, so it can be understood faster. If you can now get a pair of mahjong, please take "10 thousand" to "60 thousand" out of it, disrupt the order, and re-arrange it, just as you started playing mahjong. Do you need to take out a mahjong and insert it into another mahjong group? This is the insertion sorting. However, computers are not as intelligent as you are. You only need a few steps to sort mahjong, and computers need to compare them one by one. However, they are so fast that you don't know how clumsy it is! You can also use playing cards to try it. However, this is not the focus of the entire sorting algorithm. The focus is on the figure below. If you do not understand it, let's look at the figure (original array (int []) {5, 6, 3, 2,} in the introduction to algorithms at the end of the article. Each array is sorted, the inserted elements are the elements that are not listed previously ):
Array [I] points to array 2nd ~ For the last element, we use array [j] To represent 0th ~ The last element before I is compared with array [I] and array [j], array [j-1],... until the elements smaller than it are compared. When the comparison is terminated, array [j] points to an element smaller than the element to be inserted, while array [j + 1] and array [j + 2] are elements with the same content, therefore, we need to insert array [I] into array [j + 1].
Let's take a look at the code (carefully analyzed and understood ):
# Include <stdio. h> # include <stdlib. h> # define SIZE 10int main (int argc, char * argv []) {int I, j, temp, array [SIZE] = {, 3, 2, 7, 0}; for (I = 1; I <SIZE; I ++) {// start with sorting, and set the I initial value as the second element of the array temp = array [I]; // save this element, and then compare and sort it for (j = I-1; j> = 0 & array [j]> temp; j --) // j indicates the first element of I. After the size of array [I, find a pluggable position. array [j + 1] array [j + 1] = array [j]; // the key is to move the large element backward! Array [j + 1] = temp; // insert !!! } For (I = 0; I <SIZE; I ++) printf ("%-5d", array [I]); getch (); return 0 ;}
Note that array [0] ~ Array [I] is a sorted array. We call it a sub-array. we insert the elements after array [I] into the sub-array in sequence until no elements can be inserted, this sub-array is the entire array, sorted.
In some cases, the Code may be different from the code above. In the second loop, we use for instead of while. Because of the bubbling, selection, and sorting, simple sorting algorithms can all be represented by one for loop nested with another for loop. To facilitate memory, I used the former. The second loop uses the while code:
# Include <stdio. h> # include <stdlib. h> # define SIZE 10int main (int argc, char * argv []) {int I, j, temp, array [SIZE] = {, 3, 2, 7, 0}; for (I = 1; I <SIZE; I ++) {// The truth is the same j = I-1; temp = array [I]; while (j> = 0 & array [j]> temp) {array [j + 1] = array [j]; j --;} array [j + 1] = temp;} for (I = 0; I <SIZE; I ++) printf ("%-5d", array [I]); getch (); return 0 ;}
If you do not understand, sort the mahjong or playing cards several times by code!
Running result:
Appendix: Introduction to Algorithms