Introduction to algorithms Study Notes (2)-merge and sort, Introduction Study Notes

Source: Internet
Author: User

Introduction to algorithms Study Notes (2)-merge and sort, Introduction Study Notes

Today, I learned the Merge Sorting Algorithm in the introduction to algorithms and completed writing pseudocode on paper. I have learned how to merge and not understand it thoroughly.

I am still confused: Why is the merging and sorting more complex than the fast sorting time? Why does the database function use the fast sorting instead of merging? Now I know the reason, because merging and sorting requires extra space and large space overhead, the following describes the algorithm:

First, merge and sort uses the idea of divide and conquer. Big Data is divided into several small data, and then small data is processed separately.

Merge into big data.

Secondly, merging and sorting uses one of the most important features, that is, to combine the two sorted data into a group of ordered data, and the time of the process is

The impurity is O (n ).

Finally, the algorithm comes out. For an array A [n], we need to sort it. First, we assume A [0 ~ N/2] And A [n/2 + 1, n-1] are

Then we can sort the order in the time of O (n). However, the problem is that A [0 ~ N/2] And A [n/2 + 1, n-1] are not ordered sequences, so we have to turn them into ordered sequences. How can we change them? We then separately perform the following operations on A [0 ~ N/2] And A [n/2 + 1, n-1] can be sorted. For A [0 ~ N/2], we use the same method as above to divide it into A [0 ~ N/2/2] And A [n/2 + 1, n/2]. If the two arrays are ordered sequences, they can be merged, then, how can we determine that they are in order? When there is only one element, this element is ordered. Therefore, you only need to recursively calculate the number of elements as 1 and then return the merge result.

The following is a proof of the correctness of the merge () (merge) function in the following code:

1. when the first iteration of the loop, I = L, A [L, I-1] is empty, is an ordered sequence (null is also an ordered sequence ), and contains the smallest number of I-L = 0 LA [n1], RA [n2], then c1 = c2 = 0, LA [c1] and RA [c2] are the smallest elements in each other's array.

2. assume that LA [c1] <= RA [c2] In the I iteration, then LA [c1] is the smallest element that has not been copied to, in this case, A contains the smallest I-L elements. When A [I] = LA [c1] is executed, A contains the smallest I-L + 1 element, then add c1 and I for the next iteration. If LA [c1]> RA [c2] at the first iteration, execute a similar process.

3. After the loop ends, I = r + 1. At this time, A contains the smallest element I-L = r-L +, which happens to be l ~ All the elements of r are sorted and verified.

//insertion_sort#include <iostream>using namespace std;const int inf = (1<<28);void print(int* A, int n){    for (int i = 0; i < n; i++) {        cout << A[i] << " ";    }    cout << endl;}void merge(int *A, int l, int m, int r){    int n1 = m-l+1, n2 = r-m;    int lA[(const int)(n1+1)], rA[(const int)(n2+1)];    for (int i = 0; i < n1; i++) {        lA[i] = A[i+l];    }    for (int i = 0; i < n2; i++) {        rA[i] = A[m+1+i];    }    lA[n1] = rA[n2] = inf;    int c1 = 0, c2 = 0;    for (int i = l; i <= r; i++)    {        if (lA[c1] <= rA[c2]) {            A[i] = lA[c1++];        }        else {            A[i] = rA[c2++];        }    }}void merge_sort(int *A, int l, int r){    if (l < r)    {        int m = (l + r) / 2;        merge_sort(A, l, m);        merge_sort(A, m+1, r);        merge(A, l, m, r);    }}int main(){    int A[10] = {43,2,53,1,8,29,52,4,8,10};        cout << "before sorted: ";    print(A, 10);        merge_sort(A, 0, 10);        cout << "after sorted: ";    print(A, 10);        return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.