Class notes: insertion sort

The main operation of insert sorting is inserting.
The basic idea is: Each time a record to be sorted is inserted into an ordered sequence according to the size of its key code, until all records are sorted.
There are two kinds of insertion sorting methods: 1. Direct insertion sorting 2. Basic idea of hill sorting
direct insertion sorting : When inserting the i-th (i> 1) record, the previous i-1 records have been sorted. The key problem to be solved? (1) How to construct the initial ordered sequence? (2) How to find the insertion position of the record to be inserted? Example of direct insertion sort process





Insert picture description here

Key question (1) How to construct the initial ordered sequence?
Solution : Treat the first record as the initial ordered list, and then insert it into the ordered list from the second record in sequence until the nth record is inserted.
Algorithm description :

for (i=2; i<=n; i++) 
{       
	插入第i个记录,即第i趟直接插入排序; 
}

Key question (2) How to find the insertion position of the record to be inserted?
Solution : Insert the record r [i] in the ordered area r [1] ~ r [i-1] of the i-1 records, and search for r in order. Insert the correct position of [i], and then insert r [i] into the corresponding position.
Algorithm description :

r[0]=r[i];    
j=i-1;   
while (r[0]<r[j])  
{    
	r[j+1]=r[j];     
	j--; 
}

r [0] has two functions:
1. The value of r [i] is temporarily stored before entering the loop, so that the content of r [i] will not be lost due to the backward movement of the record;
2. In the loop to find the insertion position Act as a sentry.
Direct insertion sort algorithm

void  insertSort (int  r[ ], int n){     
	for (i=2; i<=n; i++) {         
		r[0]=r[i]; j=i-1;        
		while (r[0]<r[j]) {              
			r[j+1]=r[j];    
			j=j-1;          
		}        
		r[j+1]=r[0];     
	} 
}

The main operations in sorting: move, compare
directly insert sorting algorithm performance analysis The
best case (positive order): time complexity is O (n), the
worst case (reverse order or reverse order): time complexity is O ( n2), under
average conditions (random arrangement): time complexity is O (n2).
Space performance: A record auxiliary space is required.
The direct insertion sorting algorithm is a stable sorting algorithm.
The direct insertion sorting algorithm is simple and easy to implement, and is applicable to the case where the records to be sorted are basically ordered or the number of records to be sorted is small.
When the number of records to be sorted is large, a large number of comparison and move operations reduce the efficiency of the direct insertion sort algorithm. The basis for
Hill sorting
improvement :
(1) If the records to be sorted are basically ordered by key code, the efficiency of direct insertion sorting can be greatly improved;
(2) Because the direct insertion sorting algorithm is simple, when the number n of records to be sorted is small The efficiency is also very high.
Basic idea : Divide the entire record to be sorted into several sub-sequences, and insert and sort directly in the sub-sequences. When the records in the entire sequence are basically ordered, insert and sort all the records directly.
The key problem to be solved?
(1) How should the records to be sorted be divided to ensure that the entire sequence gradually develops to a basic order? (2) How to perform direct insertion sort within the subsequence?
What is the purpose of dividing the records to be sorted?
1. Reduce the number of records to be sorted;
2. Make the entire sequence develop to a basic order.
How to split?
The composition of subsequence can not be simply "segmented segment by segment", but the records separated by some "incremental" form a subsequence.
Example of Hill insertion sort process
Insert picture description here
Key question (1) How to divide the records to be sorted?
Solution : Group records separated by an "increment" into a subsequence.
How should the increment be taken?
The earliest method proposed by Hill is d1 = n / 2, di + 1 = di / 2.
Algorithm description :

for (d=n/2; d>=1; d=d/2)  
{      
	以d为增量,进行组内直接插入排序; 
}

Key question (2) How to perform direct insertion sort within the subsequence?
Solution : When inserting the record r [i], jump forward from r [id] (the jump amplitude is d) to search for the position to be inserted, and r [0] is only a temporary storage unit, not a sentry. When the search position <0, it means that the insertion position has been found. During the search process, the record shift is also skipped d positions. In the entire sequence, the first d records are the first records in the d subsequences, so the insertion starts from the d + 1th record.
Algorithm description :

for (i=d+1; i<=n; i++)  //将r[i]插入到所属的子序列中 
{      
	r[0]=r[i];     //暂存待插入记录       
	j=i-d;                         
	while (j>0 && r[0]<r[j])      
	{            
		r[j+d]=r[j];     //记录后移d个位置           
		j=j-d;          //比较同一子序列的前一个记录       
	}    
	r[j+d]=r[0];  
}

Hill Sort

void Shellsort(int r[],int n){   
	for (d=n/2; d>=1; d=d/2){      
		for (i=d+1; i<=n; i++){       
			r[0]=r[i];                  
			j=i-d;
			while (j>0 && r[0]<r[j])       
			{              
				r[j+d]=r[j];             
				j=j-d;                  
			}        
			r[j+d]=r[0];   
		}   
	} 
}                

Time performance of the
Hill sorting algorithm The time performance of the Hill sorting algorithm is a function of the increments taken, and so far no one has yet found a best incremental sequence. Studies have shown that the time performance of Hill sorting is between O (n2) and O (nlog2n). When n is within a certain range, the number of comparisons required for Hill sorting and the number of recorded movements are approximately O (n1.3).

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/104433077
Recommended