Note: The merge sort referred to in this article refers to the two-way merge sort.
Merge sort is a stable sorting algorithm with average, worst-case and best-case time complexity O (nlog2n). Recently, the recursive, non-recursive, and natural merge sorting algorithms of the merge sort were combed.
The basis of the merge sort: Merging two ordered arrays into an ordered array requires an O (n) of auxiliary space.
Photo from: https://www.cnblogs.com/chengxiao/p/6194356.html
Array: arrays to be sorted
Temparray: Temporary Array
startindex: Starting subscript
Middleindex: Intermediate value subscript
Endindex: Terminating subscript
void merge (int sourcearray[], int temparray[], int startindex, int middleindex, int endindex)
{
int left = startindex, index = startindex;
int right = Middleindex + 1;
if (left >= right)
Return
Merging two ordered arrays
while (left! = middleindex + 1 && Right! = endindex + 1)
{
if (Sourcearray[left] <= sourcearray[right])
{
temparray[index++] = sourcearray[left++];
}
Else
{
temparray[index++] = sourcearray[right++];
}
}
The left array is not merged and is directly joined
while (left!= middleindex+ 1)
{
temparray[index++] = sourcearray[left++];
}
The right array is not merged, directly into
while (right! = Endindex + 1)
{
temparray[index++] = sourcearray[right++];
}
Assigns an ordered array to a sorted array in a temporary array
for (int i = 0; i < endindex+ 1; ++i)
{
Sourcearray[i] = Temparray[i];
}
}
Merge sort recursive algorithm:
void mergesort (int sourcearray[],int temparray[], int low, int. high)
{
if (Low>=high)
Return
Divided
int middle = (Low+high)/2;
Left half recursive
MergeSort (Sourcearray, Temparray, Low,middle);
Right half recursion
MergeSort (Sourcearray, Temparray, Middle+1,high);
Merge the left and right halves
Merge (Sourcearray,temparray,low,middle,high);
}
Non-recursive merge sorting algorithm:
Instead of recursive ordering, an ordered array of elements and adjacent elements is formed, and an ordered array is formed with the next array until the entire array is ordered.
void merge_noncursive (int sourcearray[], int temparray[],int endindex)
{
Step, in the I+step array order, will sourcearray[i]...sourcearray[step-1] and Sourcearray[i+step]...sourcearray[min (endindex,i+2* STEP-1)] Two ordered arrays are merged together.
int step = 1;
int index;
while (step <= endindex)
{
index = 0;
Merging adjacent arrays
while (index <= endindex-2*step + 1)
{
Merge (sourcearray,temparray,index,index+step-1,index+2*step-1);
Index + = 2*step;
}
Merge ordered left half and less than one step in the right half
if (index + step <= endindex)
{
Merge (Sourcearray,temparray,index,index+step-1,endindex);
}
Step *= 2;
}
}
Natural Merge Sort:
Since merge sorting is the merging of multiple ordered arrays into an array, in addition to the fully inverted array, there is always a part of the array that is ordered, and we can get the tag of an ordered array, merging multiple ordered arrays into an ordered array.
This is the key to understanding the nature merge sort
int getindex (int array[], int flag[],int index)
{
int next = 0;
The first is subscript 0
Flag[next] = 0;
next++;
for (int i = 0;i<index;i++)
{
Finding array elements is not an orderly place
if (Array[i] > Array[i+1])
{
flag[next++] = i;
}
}
The last one is the maximum subscript
Flag[next] = index;
return next;
}
void merge_naturally (int sourcearray[],int temparray[],int index)
{
int * flag = new Int[index];
int num = GetIndex (sourcearray,flag,index);
A value greater than or equal to 2 indicates that the array is not fully ordered except for 0 with the other number
while (Num >= 2)
{
Merging adjacent ordered arrays
for (int i = 0; i<=num;i+=2)
{
Merge (sourcearray,temparray,flag[i],flag[i+1],flag[i+2]);
}
Continue to get unordered sequence numbers
num = GetIndex (sourcearray,flag,index);
}
}
Summary of algorithm for merge sort (recursive, non-recursive, and natural merge sort)