Merge sorting (MERGE-SORT) is an effective sorting algorithm based on merge operations. This algorithm is a very typical application of Divide and Conquer. Combine existing ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence in order, and then make the subsequences in order. If two ordered lists are merged into one ordered list, it is called two-way merge.
Simply put, merge sort is a relatively simple and stable algorithm.
As long as you are a friend who can call recursion, you can definitely type merge sort by hand.
The merge operation (merge), also called the merge algorithm, refers to the method of merging two sequential sequences into one sequential sequence.
Such as has a number sequence {6, 202, 100, 301, 38, 8, 1}
Initial state: 6,202,100,301,38,8,1
After the first merge: {6,202},{100,301},{8,38},{1}, the number of comparisons: 3;
After the second merging: {6,100,202,301}, {1,8,38}, the number of comparisons: 4;
After the third merge: {1,6,8,38,100,202,301}, the number of comparisons: 4;
The total number of comparisons is: 3+4+4=11,;
The reverse number is 14;
The speed is second only to quick sort, and it is a stable sorting algorithm. It is generally used for the sequence of numbers in which the overall is disordered, but the sub-items are relatively orderly.
int n, a[maxn], t[maxn];
LL ans;//Returns the reverse number of records
void Merg_Sort(int x, int y)
{
if (y - x <= 1)
return;
int mid = x + (y - x) / 2;
Merg_Sort(x, mid);
Merg_Sort(mid, y);
int p = x;
int q = mid;
int i = x;
while (p < mid || q < y)
{
if (q >= y || (p < mid&&a[p] <= a[q]))
{
t[i++] = a[p++];
}
else
{
if (p < mid)
ans += (mid - p);
t[i++] = a[q++];
}
}
for (i = x; i < y; i++)
{
a[i] = t[i];
}
}
Merge sort(Merge sort) is an effective sorting algorithm based on the merge operation. The algorithm usesDivide and conquer(Divide and Conquer) is a very typical application. The divide-and-conquer me...
Code main idea Test Results to sum up One, the code 2. Core Ideas 1. Insertion sort Compare from back to front Move the element backward while comparing When you find the position you want to insert, ...
Summary Recently, I reviewed various sorting algorithms, and recorded the learning summary and experience, I hope it can be helpful to everyone. This article introduces 9 classic sorting algorithms: b...
1. What is sorting? [1] Sort:It is to organize a group of messy data according to a certain rule (ascending or descending order). data sheet:A limited set of data elements to be sorted. Sort code:Gene...
For the sake of simplicity, an int type array is used to describe the merge algorithm, and later extended to other types of sorting. table of Contents 1.2 Sorting ideas 1.3 Seeing names 1.4 Abstract p...
Point Merge sorting is an effective sorting algorithm based on the merge operation. The algorithm usesDivide and ConquerA very typical application. Combine existing ordered subsequences to obtain a co...
Merge sorting, as the name suggests, is the process of recursive and merger. The time complexity of mergers and sorting is O (NLOGN). This algorithm adopts the classic divide-and-confer strategy. The ...
Merge sorting is an effective sorting algorithm created in the merger operation. In 1945, it was proposed by John von Neumanman for the first time. This algorithm is a very typical application of the ...
Merge sort Introduction to Merge Sort Merge sort is to useMergeThis sorting algorithm uses the classic divide-and-conquer strategy [the so-called divide-and-conquer method, which first divides the pro...
Full Stack Engineer Development Manual (Author: Luan Peng) js series tutorial 5-data structure and algorithm full solution Detailed js sorting algorithm-merge sort The merge sort can actually be compa...