From http://blog.csdn.net/cjf_iceking/article/details/7916194
After Bubble sorting and code selection and writing, the landlord gradually finds the confidence of coding, which makes perfect. Just like before singing a word, he has to recite a lot of poems and be familiar with various songs, in order to get out of your own route and have your own masterpiece. Well, let the landlord continue the task of "primary socialist phase". This is the insertion sorting.
1. Algorithm Description
Insert sorting: insert indicates to insert a new data into an ordered array and keep the data in order. For example, there is an unordered array whose length is N, the insertion of the N-1 can complete the sorting; for the first time, the number of arrays 1st is considered as an ordered array, insert the second element of the array into only one ordered array. The second time, the first two elements of the array form an ordered array, insert the third element of the array into an ordered array composed of two elements ...... the N-1 times, the array before the N-1 elements constitute an ordered array, the n elements of the array into an ordered array composed of N-1 elements, then the entire insertion sorting is completed.
Take the following five unordered data as an example:
65 27 59 64 58 (this article only details the fourth Insert Process)
1st inserts: 27 65 59 64 58
2nd inserts: 27 59 65 64 58
3rd inserts: 27 59 64 65 58
4th inserts: 27 58 59 64 65
Ii. Algorithm Analysis
Average time complexity: O (n2)
Space complexity: O (1) (used to record the data to be inserted)
Stability: Stability
3. The algorithm performs the insert sorting from the forward to the Backward Search:
[CPP]View plaincopyprint?
- /*************************************** *****************
- * Function name: insertsort
- * Parameter description: pdataarray unordered array;
- * Idatanum indicates the number of unordered data.
- * Description: insert sorting
- **************************************** *****************/
- Void insertsort (int * pdataarray, int idatanum)
- {
- For (INT I = 1; I <idatanum; I ++) // insert data starting from 2nd
- {
- Int J = 0;
- While (j <I & pdataarray [J] <= pdataarray [I]) // find the inserted position
- J ++;
- If (j <I) // before I, there is a number larger than pdataarray [I], then move and insert
- {
- Int K = I;
- Int temp = pdataarray [I];
- While (k> J) // move the position
- {
- Pdataarray [k] = pdataarray [k-1];
- K --;
- }
- Pdataarray [k] = temp; // insert
- }
- }
- }
/*************************************** * ****************** Function Name: insertsort * parameter description: pdataarray unordered array; * idatanum indicates the number of unordered data * description: insert sorting ************************************** * *****************/void insertsort (int * pdataarray, int idatanum) {for (INT I = 1; I <idatanum; I ++) // insert from 2nd data records {Int J = 0; while (j <I & pdataarray [J] <= pdataarray [I]) // find the inserted position J ++; If (j <I) // before I, if there is a number larger than pdataarray [I], move and insert {int K = I; int temp = pdataarray [I]; while (k> J) // move position {pdataarray [k] = pdataarray [k-1]; k --;} pdataarray [k] = temp; // insert }}}However, the landlord found that the method of searching and inserting from the back was less complex:
[CPP]View plaincopyprint?
- /*************************************** *****************
- * Function name: insertsort
- * Parameter description: pdataarray unordered array;
- * Idatanum indicates the number of unordered data.
- * Description: insert sorting
- **************************************** *****************/
- Void insertsort (int * pdataarray, int idatanum)
- {
- For (INT I = 1; I <idatanum; I ++) // insert data starting from 2nd
- {
- Int J = I-1;
- Int temp = pdataarray [I]; // record the data to be inserted
- While (j> = 0 & pdataarray [J]> temp) // from the back to the front, locate its smaller number
- {
- Pdataarray [J + 1] = pdataarray [J]; // move backward
- J --;
- }
- If (J! = I-1) // a smaller number
- Pdataarray [J + 1] = temp;
- }
- }
/*************************************** * ****************** Function Name: insertsort * parameter description: pdataarray unordered array; * idatanum indicates the number of unordered data * description: insert sorting ************************************** * *****************/void insertsort (int * pdataarray, int idatanum) {for (INT I = 1; I <idatanum; I ++) // insert from 2nd data records {Int J = I-1; int temp = pdataarray [I]; // record the data to be inserted while (j> = 0 & pdataarray [J]> temp) // forward from the back, find the location of a smaller number than it {pdataarray [J + 1] = Pd Ataarray [J]; // move j --;} If (J! = I-1) // There are smaller pdataarray [J + 1] = temp ;}}4. in algorithm-optimized insertion sorting, the insert position is always searched first, and then the insertion and moving process is performed. The insert position is queried in sequence (from the front to the back or from the back to the back ), since the array to be inserted is already ordered, you can use the binary search method to find the Insertion Location to improve the algorithm efficiency, but the time complexity of the algorithm is still O (n2 ).
[CPP]View plaincopyprint?
- // Find the position of idata in the pdataarray array with the ilen Length
- Int findinsertindex (int * pdataarray, int ilen, int idata)
- {
- Int ibegin = 0;
- Int iend = ilen-1;
- Int Index =-1; // record the insert position
- While (ibegin <= iend)
- {
- Index = (ibegin + iend)/2;
- If (pdataarray [Index]> idata)
- Iend = index-1;
- Else
- Ibegin = index + 1;
- }
- If (pdataarray [Index] <= idata)
- Index ++;
- Return Index;
- }
- /*************************************** *****************
- * Function name: binaryinsertsort
- * Parameter description: pdataarray unordered array;
- * Idatanum indicates the number of unordered data.
- * Description: Binary Search insertion sorting
- **************************************** *****************/
- Void binaryinsertsort (int * pdataarray, int idatanum)
- {
- For (INT I = 1; I <idatanum; I ++) // insert data starting from 2nd
- {
- Int Index = findinsertindex (pdataarray, I, pdataarray [I]); // binary search for the inserted position
- If (I! = Index) // The insert position is not I before moving or inserting
- {
- Int J = I;
- Int temp = pdataarray [I];
- While (j> index) // move the location
- {
- Pdataarray [J] = pdataarray [J-1];
- J --;
- }
- Pdataarray [J] = temp; // insert
- }
- }
- }
Sort by insert