From an array of random order, the values are inserted into an array that is already sorted. This may seem like a two array to complete, but it is also possible if you want to sort only within the same set of numbers. At this point you need to imagine two areas: the front ordered area and the rear chaos sequence area.
Set the array to a[0...n-1].
- Initially, a[0] self into 1 ordered areas, unordered area is a[1...n-1]. Make I=1
- Incorporating A[i] into the ordered interval of A[0...I] formed in the current ordered region A[0...i-1].
- i++ and repeat the second step until the i==n-1, sorting is complete.
The following is a code that is written strictly by definition (ordered from small to large):
1 voidInsertSort1 (intData[],intlen)2 {3 intI, J, K;4 for(i =1; i < Len; i++)5 {6 //find a suitable position for data[i] in the orderly interval of the front data[0...i-1]7 for(j = i-1; J >=0; j--)8 if(Data[j] < data[i])//when a location is found, stop9 Break;Ten One //If you find a suitable location, A if(J! = i-1) - { - //move data that is larger than data[i] back the inttemp =Data[i]; - for(k = i-1; k>j; k--) -Data[k +1] =Data[k]; - + //after executing the FOR loop above, K=j, and because Temp>data[j], place temp in the back of the ordinal J -Data[k +1] =temp; + } A } at}
This code is too long, not clear enough. Now make the rewrite, merging the search and data back two steps. That is, each data[i] first and the previous data data[i-1] (because of the unordered interval, I starting from 1) compared, if data[i]>data[i-1], the description data[0...i] is also ordered, without adjustment. Otherwise, make j=i-1,temp=data[i]. Then, while moving the data data[j] backward, search forward, stop when there is data data[j]<data[i], and place temp at data[j+1].
1 /*2 @ Merge the search and data back two steps3 */4 voidInsertSort2 (intData[],intlen)5 {6 intI, J;7 for(i =1; I < len;i++)8 if(Data[i] < data[i-1])//when an element in an unordered interval is more than an hour in an ordered interval, the description needs to be adjusted9 {Ten inttemp = Data[i];//temporarily store the element you want to adjust One for(j = i-1; J >=0&& data[j]>temp; j--)//when there is data data[j]<data[i] (i.e. data[j]<temp) stop and drop temp to data[j+1] AData[j +1] =Data[j]; -Data[j +1] =temp; - } the}
You can also rewrite the method used to insert data[j] into the ordered interval of the previous data[0...j-1], using data exchange instead of data back-shift. If DATA[J] "previous data data[j-1]>data[j], swap data[j] and data[j-1], and j--know Data[j-1]<=data[j]. This allows new data to be merged into an ordered interval.
1 /*2 @ Use data exchange instead of data to move back3 */4 voidINSERTSORT3 (intData[],intlen)5 {6 intI, J;7 for(i =1; I < len;i++)8 for(j = i-1; J >=0&& Data[j]>data[j +1]; j--)9Swap (Data[j], data[j +1]);Ten}
Full code
1 /*2 @theme: Simple Insert Sort3 @author: Codingmengmeng4 @date: 2016-11-10 12:16:325 @email: [email protected]6 */7#include <iostream>8 using namespacestd;9 /*Ten @ code written in strict accordance with the definition One */ A voidInsertSort1 (intData[],intlen) - { - intI, J, K; the for(i =1; i < Len; i++) - { - //find a suitable position for data[i] in the orderly interval of the front data[0...i-1] - for(j = i-1; J >=0; j--) + if(Data[j] < data[i])//when a location is found, stop - Break; + A //If you find a suitable location, at if(J! = i-1) - { - //move data that is larger than data[i] back - inttemp =Data[i]; - for(k = i-1; k>j; k--) -Data[k +1] =Data[k]; in - //after executing the FOR loop above, K=j, and because Temp>data[j], place temp in the back of the ordinal J toData[k +1] =temp; + } - } the } * $ /*Panax Notoginseng @ Merge the search and data back two steps - */ the voidInsertSort2 (intData[],intlen) + { A intI, J; the for(i =1; I < len;i++) + if(Data[i] < data[i-1])//when an element in an unordered interval is more than an hour in an ordered interval, the description needs to be adjusted - { $ inttemp = Data[i];//temporarily store the element you want to adjust $ for(j = i-1; J >=0&& data[j]>temp; j--)//when there is data data[j]<data[i] (i.e. data[j]<temp) stop and drop temp to data[j+1] -Data[j +1] =Data[j]; -Data[j +1] =temp; the } - }Wuyi the /* - @ Use data exchange instead of data to move back Wu */ - voidINSERTSORT3 (intData[],intlen) About { $ intI, J; - for(i =1; I < len;i++) - for(j = i-1; J >=0&& Data[j]>data[j +1]; j--) -Swap (Data[j], data[j +1]); A } + intMainvoid) the { - intLen//to sort the length of an array $cout <<"the length of the array to be sorted is:"<<Endl; theCIN >>Len; the int* Data = (int*)malloc(Len) * (sizeof(int)));//dynamically allocating an array of type int of len size thememset (data,0, (LEN) *sizeof(int));//the value of the initialized array is 0, otherwise an error occurs the - //read in Data incout <<"Please enter"<< Len <<"of data:"<<Endl; the for(inti =0; i < Len; i++) theCIN >>Data[i]; About insertSort3 (data, Len); the //results after sorting the output thecout <<"insert a sorted result:"<<Endl; the for(inti =0; i < Len; i++) +cout << Data[i] <<" "; - the return 0;Bayi}
Operation Result:
Sort algorithm (v) Insert sort