Code and ideas for merging and sorting, and ideas for merging and sorting

Source: Internet
Author: User

Code and ideas for merging and sorting, and ideas for merging and sorting

First, consider how to merge the two ordered series. This is very simple, as long as we compare the first number of two columns, who is the first to take the first, and then delete the number in the corresponding series. Then compare the data. If the number of columns is null, extract the data of the other series in sequence.

Copy codeThe Code is as follows:
View Code
// Merge the ordered arrays a [] and B [] into c []
Void MemeryArray (int a [], int n, int B [], int m, int c [])
{
Int I, j, k;

I = j = k = 0;
While (I <n & j <m)
{
If (a [I] <B [j])
C [k ++] = a [I ++];
Else
C [k ++] = B [j ++];
}

While (I <n)
C [k ++] = a [I ++];

While (j <m)
C [k ++] = B [j ++];
}

It can be seen that the efficiency of merging ordered series is relatively high, which can reach O (n ).

The preceding merge sequence problem is solved. Let's look at the merge sequence. The basic idea is to divide the array into two groups A and B. If the data in these two groups is ordered, this makes it easy to sort the two groups of data. How can we make the data in these two groups orderly?

Group A and Group B can be further divided into two groups. And so on. When the split group has only one data, you can think that the group has reached an order, and then merge the two adjacent groups. In this way, the merging order is completed by recursively decomposing the series and then merging the series.

Copy codeThe Code is as follows:
View Code
// Merge two ordered Series a [first... mid] And a [mid... last.
Void mergearray (int a [], int first, int mid, int last, int temp [])
{
Int I = first, j = mid + 1;
Int m = mid, n = last;
Int k = 0;

While (I <= m & j <= n)
{
If (a [I] <= a [j])
Temp [k ++] = a [I ++];
Else
Temp [k ++] = a [j ++];
}

While (I <= m)
Temp [k ++] = a [I ++];

While (j <= n)
Temp [k ++] = a [j ++];

For (I = 0; I <k; I ++)
A [first + I] = temp [I];
}
Void mergesort (int a [], int first, int last, int temp [])
{
If (first <last)
{
Int mid = (first + last)/2;
Mergesort (a, first, mid, temp); // order on the left
Mergesort (a, mid + 1, last, temp); // ordered on the right
Mergearray (a, first, mid, last, temp); // merge the two ordered series.
}
}

Bool MergeSort (int a [], int n)
{
Int * p = new int [n];
If (p = NULL)
Return false;
Mergesort (a, 0, n-1, p );
Delete [] p;
Return true;
}

The efficiency of merging and sorting is relatively high. If the length of a series is set to N, splitting the series into small series requires a total of N logN steps. Each step is a process of merging ordered series, the time complexity can be recorded as O (N), so a total of O (N * logN ). Because Merge Sorting is performed on adjacent data each time, Merge Sorting is performed in several sorting methods (fast sorting, Merge Sorting, Hill sorting, and heap sorting) of O (N * logN) it is also highly efficient

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.