Sorting algorithm analysis (in-line, half, hill)

1. Sorting classification
According to the different memory occupied by the data when sorting, the sorting can be divided into two categories. One is that the entire sorting process is performed entirely in memory , which is called internal sorting; the other is that because the amount of data to be sorted is too large, the memory cannot hold all the data, and the sorting needs to be completed with the help of an external storage device , which becomes an external sorting.
2. Insertion sorting

The basic idea of ​​insertion sort : on the basis of a sorted subset of records, each step inserts the next record to be sorted into the sorted subset of records in an orderly manner, until all the records to be sorted are inserted until.

2.1 Direct insertion sorting
example: the sorting sequence to be sorted is {48, 62, 35 , 77, 55, 14, 35 , 98}
. The complete direct insertion sorting process implementation example, the curly brackets are the currently sorted record subset
A ) {48} 62 35 77 55 14 35 98
B) {48 62} 35 77 55 14 35 98
C) {35 48 62} 77 55 14 35 98
D) {35 48 62 77} 55 14 35 98
E) { 35 48 55 62 77} 14 35 98
F) {14 35 35 48 55 62 77} 98
G) {14 35 35 48 55 62 77} 98
H) {14 35 35 48 55 62 77 98 }
Assuming that records to be sorted are stored In r[1...length] , in order to improve efficiency, set a monitoring post r[0] so that r[0] always stores the record to be inserted. The two functions of the surveillance post: ①Back up the records to be inserted, so that the records with larger keywords in the front can be moved backward; ②The implementation of the algorithm to prevent out-of-bounds

void InsSort(RecordType r[],int length)//对记录数组r做直接插入排序,length为数组中待排序记录的数目

{
    
    
for(i=2;i<=length;i++)
{
    
    
r[0]=r[i];j=i-1;//将待插入记录存放到监视哨r[0]中
while(r[0].key<r[j].key)//寻找插入位置
{
    
    
r[j+1]=r[j];j=j-1;
}
r[j+1]=r[0];//将待插入记录插入到已排序的序列中
}
}

Key points of the algorithm: ①Set the monitoring post r[0] to temporarily save the record to be inserted. ②Find the position that should be inserted from the back to the front. ③Search and move the same loop to complete the
algorithm analysis: ①Time complexity O(n²)②Best case O(n), n is small or the elements are basically ordered ③Worst case O(n²)④Space complexity O(1)⑤Stable **while(r[0].key<r[j].key)* *Guaranteed that the same rear elements will not be arranged to the front

2.2 fold half insertion sort

For an ordered list, the performance is better than the sequential search. The idea of ​​the binary search can be used to determine the insertion position in the ordered record r[1...i-1]

Algorithm implementation:

void BinSort(RecordType r[],int length)
//对记录数组r进行折半插入排序,length为数组的长度
{
    
    
for(i=2;i<=length;i++)
{
    
    
x=r[i];
low=1;high=i-1;
while(low<=high)//确定插入位置
{
    
    
mid=(low+high)/2;
if(x.key<r[mid].key) 
high = mid-1;
else
low = mid+1;
}
for(j=i-1;j>=low;--j)  r[j+1]=r[j];//记录依次向后移动
r[low]=x; //插入记录
}
}

Key points of the algorithm: The binary insertion sort can reduce the number of comparisons of keywords. For each element inserted, the maximum number of comparisons required is the deep
algorithm analysis of the binary decision tree : ①The time complexity is O(n²), and the comparison time complexity is O( nlogn)②Best case O(nlogn)③Worst case O(n²)④Space complexity O(1)

2.3 Hill sort

Hill sorting, also known as reduced incremental sorting, is based on the idea of ​​insertion and has the best properties of direct insertion

Example: The sequence to be sorted is {46,55,13,42,94,17,05,70} to give the execution process of the Hill sorting algorithm

Algorithm implementation:

void ShellInsert(RecordType r[],int length,int delta)
//对记录数组r做一趟希尔插入排序,length为数组的长度,delta为增量
{
    
    
for(i=1+delta;i<=length;i++)
//1+delta为第一个子序列的第二个元素的下标
if(r[i].key<r[i-delta].key)
{
    
    
r[0]=r[i];
for(j=i-delta;j>0 && r[0].key<r[j].key;j-=delta)
r[j+delta]=r[j];
r[j+delta]=r[0];
}
}

void ShellSort(RecordType r[],int length,int delta[],int n)
//对记录数组r做希尔排序,length为数组r的长度,delta为增量数组,n为delta[]的长度
{
    
    
for(i=0;i<=n-1;++i)
ShellInsert(r,Length,delta[i]);
}

Key points of the algorithm: When the interval between the subsequences is d, there are d subsequences, and the d subsequences need to be inserted and sorted separately. However, when the algorithm is implemented in detail, it is not the first to complete the insertion of one subsequence, and then insert the other subsequence. Instead, start from the second record of the first subsequence and scan the entire sequence of records to be sorted. Which subsequence the current record belongs to will be inserted in which subsequence, so the algorithm will be in each subsequence Insert sort

Algorithm analysis: ①Time complexity O(n 1.5), space complexity O(1) ②Unstable eg: {2 4 1 2 }

Reference book: "Data Structure" Second Edition————Geng Guohua

Guess you like

Origin blog.csdn.net/qq_45657288/article/details/105947087