Merge linear algorithm sorting and merge linear algorithms

Source: Internet
Author: User

Merge linear algorithm sorting and merge linear algorithms
Sort category

Many people are not very clear about sorting and classification, but I can express my own opinions. There are two types of sorting algorithms: Internal sorting and external sorting. Internal sorting generally refers to the Sorting Algorithm of data in the memory, and the data volume is generally small. The maximum is no more than 2 GB. After all, this occupies all the user process addresses. In fact, it is impossible to get stuck. External sorting generally refers to the storage of data in files. It is not ruled out that data is stored in the database. The internal and external sorting links are here. Well-known merger sorting.

Merge

External sorting first divides the data into some data segments that can be loaded into the memory. Each data segment is sorted first, and finally, it is sorted and stored in the file again by means of Merge Sorting. There can be many ways to merge and sort. As a research article, it is generally the most typical two-way sorting. If there is more, it needs to be related to the demand. Unfortunately, we do not know the data volume, so we use a single-linked table node. Let's take a look at the implementation of merge.

Implementation

# Include <stdio. h> # include <malloc. h> # include <memory. h> /************************************* * ********************************** @ linked list node @ data -- node data @ next -- next node pointer ******************************* **************************************** */typedef struct node {int data; struct node * next;} node; /*************************************** * ********************************* @ *** ************************* **************************************** * ***/Int node_insert (struct node * the_node, int data) {// find the final node struct node * cur_node = the_node; while (cur_node-> next! = NULL) {cur_node = cur_node-> next;} // create a new node, struct node * new_node = (struct node *) malloc (sizeof (struct node) * 1 ); new_node-> data = data; new_node-> next = NULL; // Add a new node, cur_node-> next = new_node; return 0 ;} /*************************************** ********************************* @ release linked list **** **************************************** * **************************/int node_free (struct node * the_node) {struct node * cur_node = NUL L; while (the_node! = NULL) {cur_node = the_node; the_node = the_node-> next; free (cur_node); cur_node = NULL;} return 0 ;} /*************************************** * ********************************** @ linked list merging **** **************************************** * **************************/int list_merge (struct node * first, struct node * second, struct node ** result) {// record the operation node struct node ** cur_result = result; // select a small while (first! = NULL & second! = NULL) {if (first-> data <second-> data) {* cur_result = first; cur_result = & (* cur_result)-> next ); first = first-> next;} else {* cur_result = second; cur_result = & (* cur_result)-> next); second = second-> next ;}} // process the remaining if (first! = NULL) {* cur_result = first;} // process the remaining if (second! = NULL) {* cur_result = second;} return 0 ;} /*************************************** * ******************************** @ main function-program entry point **************************************** * ******************************/int main () {// create the first linked list struct node * first = (struct node *) malloc (sizeof (struct node) * 1); first-> data = 1; first-> next = NULL; for (int I = 2; I <10; I ++) {node_insert (first, I );} // create the second linked list struct node * second = (struct node *) m Alloc (sizeof (struct node) * 1); second-> data = 2; second-> next = NULL; for (int I = 14; I <100; I ++) {node_insert (second, I);} // merge and sort struct node * result = NULL; list_merge (first, second, & result ); // output result struct node * out_result = result; while (out_result! = NULL) {printf ("% d ___", out_result-> data); out_result = out_result-> next;} // release the node_free (result); result = NULL; first = NULL; second = NULL; return 0 ;}

Running result

See the picture

Summary

1. The core of the two-way merge is the while loop in merge. Compare the size.

2. the processing of the remaining data again is still a while loop. We do not know how many pieces of data are left behind after all.

3. Others are typical data structures of a single-chain table.


Merge Sorting

First, consider a simple question: how to merge two ordered queues into one ordered queue (and output) in a linear time )?

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9

As shown in the above example, the AB sequence is already ordered. When data is given in an orderly manner, we will find a lot of magical things. For example, the first number we will output must come from the number at the beginning of each of the two sequences. If the two numbers are both 1, we can retrieve one (for example, the one in queue A) and output:

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9
Output: 1

Note: We have taken out a number and deleted it from the original series. The delete operation is implemented by moving the first pointer of the team, otherwise the complexity is high.
Now, the number of headers in queue A is 3, and the number of headers in queue B is still 1. At this point, we can compare the values 3 and 1 and output a small number:

Queue A: 1 3 5 7 9
B queue: 1 2 7 8 9
Output: 1 1

The following steps:

A queue: 1 3 5 7 9 A queue: 1 3 5 7 9 A queue: 1 3 5 7 9 A queue: 1 3 5 7 9
B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ==> B queue: 1 2 7 8 9 ......
Output: 1 1 2 output: 1 1 2 3 output: 1 1 2 3 5 output: 1 1 2 3 5 7

I hope you understand how this is done. This is obviously correct, and the complexity is obviously linear.

Merge Sort will use the Merge operation mentioned above. A series is given, and the series are sorted from small to large in the time of O (nlogn) using the merge operation. Merge Sorting uses the idea of Divide and Conquer. First, we divide the given series equally into the left and right sections, then sort the two series separately, and finally use the merge algorithm to sort the two segments (sorted) merge a series into a series. Someone will ask "What sort is used for sorting the numbers of the left and right columns separately? The answer is: sort by merging. That is to say, we recursively divide each series into two sections for the above operation. You don't need to worry about how it actually works. Our program code will call this process recursively until the series cannot be divided (only one number.
When I first looked at this algorithm, some people mistakenly thought that the time complexity was quite high. The following figure provides a non-recursive view of the actual operation process of Merge Sorting for your reference. We can use this graph to prove that the time complexity of the Merge Sorting Algorithm is O (nlogn ).

[3] [1] [4] [1] [5] [9] [2] [7]
\/\/\/\/
[1 3] [1 4] [5 9] [2 7]
\/\/
[1 1 3 4] [2 5 7 9]
\/
[1 1 2 3 4 5 7 9]

Each "\/" in indicates the linear time merge operation described above. 4 rows are used to illustrate the Merge Sorting. If there are n numbers, it indicates that O (logn) rows are required. The total complexity of merge operations for each row is O (n), so the total complexity of logn rows is O (nlogn ). This is equivalent to analyzing the complexity of merging and sorting by using the recursive tree method. Assume that the complexity of Merge Sorting is T (n), and T (n) is composed of two T... the remaining full text>

Merge Sorting algorithms (divide and conquer)

What you wrote is too messy. It's not exactly what he said about missing data. However, the boundary is completely problematic, resulting in errors in all the data in the array. It looks messy. A more standardized example is provided for your reference.
Void merge (int a [], int temp [], int Lpos, int Rpos, int RightEnd)
{
Int I, LeftEnd, num, tmppos;

LeftEnd = Rpos-1;
Tmppos = Lpos;
Num = RightEnd-Lpos + 1;
While (Lpos <= LeftEnd & Rpos <= RightEnd)
If (a [Lpos] <= a [Rpos]) temp [tmppos ++] = a [Lpos ++];
Else temp [tmppos ++] = a [Rpos ++];
While (Lpos <= LeftEnd) temp [tmppos ++] = a [Lpos ++];
While (Rpos <= RightEnd) temp [tmppos ++] = a [Rpos ++];
For (I = 0; I <num; I ++)
{
A [RightEnd] = temp [RightEnd];
RightEnd --;
}
}

Void msort (int a [], int temp [], int L, int r)
{
Int c;

If (L <r)
{
C = (L + r)/2;
Msort (a, temp, L, c );
Msort (a, temp, c + 1, r );
Merge (a, temp, L, c + 1, r );
}
}

Void mergesort (int a [], int n) // call this function. a is a Sort array and n is the number of arrays.
{
Int temp [MAXN]; // MAXN indicates the maximum number of arrays that may appear in a question.

Msort (a, temp, 0, n-1 );
}

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.