Sorting detailed explanation: merge sort

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];
    }
}

Intelligent Recommendation

Detailed explanation of merge sort recursion

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...

Insert, quick sort, heap sort, merge, count and bucket sorting detailed explanation and testing

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, ...

Detailed explanation of nine classic sorting algorithms (bubble sort, insertion sort, selection sort, quick sort, merge sort, heap sort, count sort, bucket sort, radix sort)

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...

Detailed explanation of seven common sorting algorithms (direct insertion, hill, selection, bubbling, fast, merge, heap sort)

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...

Detailed explanation of merge sorting, Java version description.

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...

More Recommendation

Classic sorting algorithm (5)-Merge detailed explanation

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 Java implementation (detailed explanation)

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 ...

5. Merge sorting algorithm detailed explanation

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 ...

Super detailed sorting algorithm (6) merge sort

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...

Detailed js sorting algorithm-merge sort

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top