1. Insert sort-Direct insert sort (straight insertion sort)
Basic idea:
Insert a record into the sorted ordered table to get a new, sequential table with a 1 increase in the number of records. That is, the 1th record of the sequence is considered an ordered subsequence, and then inserted from the 2nd record one after the other until the entire sequence is ordered.
Important: Set up Sentinel as a temporary storage and judgment array boundary.
Example of a direct insert sort:
If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.
The implementation of the algorithm:
- void print (int a[], int n,int i)
- {
- Cout<<i <<":";
- For (int j= 0; j<10; j + +)
- {
- COUT<<A[J] <<"";
- }
- cout<<endl;
- }
- void Insertsort (int a[], int n)
- {
- For (int i= 1; i<n; i++)
- {
- if (A[i] < a[i-1])
- { //If the first element is greater than the i-1 element, insert it directly. Less then, move the ordered table after inserting
- int j= i-1;
- int x = a[i]; //Copy as Sentinel, which stores the elements to be sorted
- A[i] = a[i-1]; //Move an element successively
- While (x < a[j])
- { //Find the insertion position in an ordered table
- A[J+1] = A[j];
- j--; //element move back
- }
- A[J+1] = x; //Insert to the correct position
- }
- Print (a,n,i); //Print the results of each trip sort
- }
- }
- int main ()
- {
- int a[10],i;
- for (i=0; i <; i++)
- {
- A[i] = rand ()% 100;
- }
- Insertsort (a,10);
- Print (a,10,10);
- }
Efficiency:
Time complexity: O (n^2).
The other insert sort has two-insert sort, 2-way insert sort.
2. Insert sort-Hill sort (Shell ' s sort)
The hill sort was proposed by D.l.shell in 1959, and the relative direct ranking has been greatly improved. Hill sort is also called narrowing the incremental sort
Basic idea:
First, the entire sequence of records to be sorted into a number of sub-sequences of the direct insertion of the order, the entire series of records in the "Basic order", then the whole record is inserted in order.
Operation Method:
- Select an incremental sequence T1,T2,...,TK, where ti>tj,tk=1;
- According to the number of increment series K, the sequence is sorted by K-trip;
- Each order, according to the corresponding increment ti, the backlog sequence is divided into several sub-sequences of length m, respectively, the sub-table is directly inserted sort. Only the increment factor is 1 o'clock, the entire sequence is treated as a table, and the length of the table is the length of the entire sequence.
Example of a hill sort:
Algorithm implementation:
We simply handle the increment sequence: Delta sequence d = {N/2, N/4, N/8 ..... 1} n is the number of digits to be sorted
That is: A set of records to be sorted by an increment D(the number of n/2,n to be sorted) into several sets of sub-sequences, each group of records of the subscript difference D. Direct Insert sorting of all elements in each group, followed by a smaller increment ( D/2) to group it, and then make a direct insert sort in each group. Continue to shrink the increment until it is 1, and finally use the direct insert sort to complete the sorting.
- void print (int a[], int n,int i)
- {
- Cout<<i <<":";
- For (int j= 0; j<10; j + +)
- {
- COUT<<A[J] <<"";
- }
- cout<<endl;
- }
- /* directly into the general form of the sort, int dk Shrinks the increment, if the direct insertion sort, dk=1 */
- void Shellinsertsort (int a[], int n, int dk)
- {
- For (int i= dk; i<n; ++i)
- {
- if (A[i] < A[I-DK])
- { //If the first element is greater than the i-1 element, insert it directly. Less then, move the ordered table after inserting
- int j = I-DK;
- int x = a[i]; //Copy as Sentinel, which stores the elements to be sorted
- A[i] = A[I-DK]; //First move back one element
- While (x < a[j])
- {//Find the insertion position in an ordered table
- A[J+DK] = A[j];
- J-= DK; //element move back
- }
- A[J+DK] = x; //Insert to the correct position
- }
- Print (A, n,i);
- }
- }
- /* First sort by increment D (N/2,n for the number of orders to sort */
- void Shellsort (int a[], int n)
- {
- int DK = N/2;
- While (DK >= 1)
- {
- Shellinsertsort (A, n, DK);
- DK = DK/2;
- }
- }
- int main ()
- {
- int a[10],i;
- for (i=0; i <; i++)
- {
- A[i] = rand ()% 100;
- }
- Shellsort (a,10); //Hill Insert sort
- Print (a,10,10);
- }
It is difficult for hill to sort the time-lapse analysis, the comparison number of key code and the number of record moves depend on the selection of increment factor sequence d, which can accurately estimate the comparison number of key codes and the number of movement of records in certain cases. At present, no one has given a method to select the best increment factor sequence. increment factor sequence can have a variety of ways, there are odd, but also take prime numbers, but need to note: The increment factor in addition to 1 there is no public factor, and the last increment factor must be 1. The hill sort method is an unstable sort method.
Algorithm for direct insertion of sort and hill sort