Knowledge Point Summary Report
Knowledge Points:
Merge sort
(principle) Merge sort is the merging of two or more two ordered tables into a new ordered table multiple times. The simplest merging is to merge two ordered sub-tables into an ordered table, namely two-way merging.
The basic idea of the two-way merge sort is to r[0..n-1] into an ordered sequence of n length 1, then 22 to merge, to get the ordered sequence of |ˉn/2ˉ| length 2 (the length of the last ordered sequence may be 2), then 22 merge, Get |ˉn/4ˉ| Sequential sequence of lengths (the length of the last ordered sequence may be less than 4), ... Until an ordered sequence of length n is obtained.
An algorithm for merging two ordered tables directly into an ordered table
void Merge (RecType r[],int low,int mid,int High)//merge R[low. High]//an algorithm that merges two ordered tables directly into an ordered table
{RecType *r1;
int i=low,j=mid+1,k=0; K is the subscript of the R1, and I J is the subscript of the 1th and 2 paragraphs respectively.
r1= (RecType *) malloc ((high-low+1) *sizeof (RecType)); Dynamically allocating space
while (I<=mid &&j
if (r[i].key<=r[j].key)//elements in the 1th paragraph are put into R1
{R1[k]=r[i];
i++;k++;
}
else//elements in the 2nd paragraph are put into R1
{R1[K]=R[J];
j++;k++;
}
while (I<=MID)//Copy the remainder of the 1th paragraph to the R1
{R1[k]=r[i];
i++;k++;
}
while (J<=high)//Copy the remainder of the 2nd paragraph to the R1
{R1[K]=R[J];
j++;k++;
}
for (k=0,i=low;i<=high;k++,i++)//copy R1 to R[low: High] In
R[I]=R1[K];
Free (R1);
}
An algorithm for merging a trip
void Merpass (RecType r[],int length,int N)//The entire sort sequence is merged
{int i;
for (i=0;i+2*length-1<n;i=i+2*length)//merge length two adjacent child table
Merge (r,i,i+length-1,i+2*length-1);
if (i+length-1<n-1)//Two remaining sub-tables, the latter of which are less than length
Merge (r,i,i+length-1,n-1); Merge these two sub-tables
}
Two-way merger
Bottom-up algorithm in two-way merging
void MergeSort (RecType r[],int N)//Two-way merge sort
{int length;
for (length=1;length<n;length=2*length)//|ˉlog2nˉ|
Mergepass (R,length,n);
}
Top-down algorithm in two-way merging
void Mergesortdc (RecType r[],int low,int High)//to R[low. High] for two-way merge sort
{int mid;
if (Low
{mid= (Low+high)/2;
MERGESORTDC (R,low,mid);
MERGESORTDC (R,mid+1,high);
Merge (R,low,mid,high);
}
}
void MergeSort1 (RecType r[],int N)//top-down two-way merge algorithm
{MERGESORTDC (r,0,n-1);
}
Heap Sort
(principle) heap sorting is a sort of tree-type selection method. It is characterized by the R[1..N] (R[i] of the key word ki) as a complete binary tree of the sequential storage structure. Select the largest (or smallest) element of the keyword in the unordered region using the positional relationship between the parent node and the child's node in the complete binary tree.
(1) Meet ki<=k2i and ki<=k2i+1 for small Gan. Is the keyword of any node in the tree that is smaller than the key of its child node.
(2) satisfies ki>=k2i and ki>=k2i+1 (1<=i<=|_n/2_|) is a large root heap. Is the keyword in any node in the tree that is greater than or equal to its child's node.
Sorting algorithms
The key to heap sorting is to filter, the process is if the root node of the complete binary tree is r[i], its left and right sub-tree is a large heap, its two children of the keyword R[2i].key, r[2i+1].key the largest and r[i].key comparison. If the r[i].key is smaller, swap it with the largest child, which could damage the next level of heap. Continue to construct the next level of heap using the above method until the complete binary tree becomes a large heap.
Suppose to R[low. High] to filter, must satisfy R[low] is the root node of the Saozi right subtree are large root heap, its filtering algorithm sift () is as follows
void Sift (RecType r[],int low,int High)
{int i=low,j=2*i; R[J] is the left child of R[i]
RecType Tmp=r[i];
while (J<=high)
{if (J
j + +;
if (Tmp.key<r[j].key)//Joghen node is less than the maximum child's keyword
{R[I]=R[J]; Adjust the r[i] to the parent node position.
I=j; Modify the I and J values to continue filtering down
J=2*i;
}
else break; Joghen node is greater than or equal to max child keyword, filter end
}
r[i]=tmp; The filtered node is placed in the final position.
}
Implementing a heap sorting algorithm
void Heapsort (RecType r[],int N)
{int i;
for (i=n/2;i>=1;i--)//loop build initial heap, call sift algorithm |_n/2_| times
Sift (r,i,n);
for (i=n;i>=2;i--)//n-1 to complete heap sequencing, minus 1 elements per trip heap
{swap (r[1],r[i]); Swap the last element with the root r[1]
Sift (r,1,i-1); Filter R[1..i-1] To get a heap of i-1 nodes
}
}
Merge sort and Heap sort