& Quot; & quot; insert sorting, insert sorting

Source: Internet
Author: User

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

 

 

Related Article

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.