# Sort (6) --------- merge sort (implemented in C)

Source: Internet
Author: User

Merge Sorting:

Merge Operations, also known as merge algorithms, are operations that combine two sorted sequences into one. The Merge Sorting Algorithm depends on the merge operation.

The merge operation process is as follows:
(1) apply for a space in which the size is the sum of two sorted sequences. The space is used to store the merged sequence.
(2) set two pointers. The initial positions are the starting positions of the two sorted sequences respectively.
(3) Compare the elements pointed to by the two pointers, select a relatively small element and put it in the merging space, and move the pointer to the next position.
(4) Repeat Step 3 until a pointer reaches the end of the sequence.

(5) directly copy (copy) all the remaining elements of the other sequence to the end of the merged sequence.

Simply put, a sequence is continuously divided into two parts (of course, it can also be divided into multiple parts), then recursion, and then merge.

Source code:

```/* ===================================================== ================================================ # # FileName: merge_Sort.c # Desc: Merge Sorting # Author: zhangyuqing # Created: 23:18:16, January 1, June 29, 2014 # Version: 0.0.1 #================================================= ======================================================= * /# include "stdafx. h "# include

# Include

// Merge void merge (int src [], int des [], int low, int high, int mid) {int I = low; int k = low; int j = mid + 1; while (I <= mid) & (j <= high) {if (src [I] <src [j]) {des [k ++] = src [I ++];} else {des [k ++] = src [j ++] ;}} while (I <= mid) {des [k ++] = src [I ++];} while (j <= high) {des [k ++] = src [j ++] ;}} void MSort (int src [], int des [], int low, int high, int max_size) {int mid = (low + high)/2; if (low = high) {Des [low] = src [low];} else {int mid = (low + high)/2; int * des_space = (int *) malloc (sizeof (int) * max_size); if (NULL! = Des_space) {MSort (src, des_space, low, mid, max_size); MSort (src, des_space, mid + 1, high, max_size); merge (des_space, des, low, high, mid) ;}free (des_space) ;}void Meger_Sort (int arr [], int low, int high, int len) {MSort (arr, arr, low, high, len) ;}int main (void) {int arr [10]; for (int I = 0; I <10; I ++) // initialization data {arr [I] = rand () % 100; // randomly generated data} printf ("Before sort: \ n "); // print the data before sorting for (int I = 0; I <10; I ++) {printf ("% d", arr [I]);} // start sorting Meger_Sort (arr, 0, 10-1, 10); printf ("\ nAfter sort: \ n "); // print the sorted data for (int I = 0; I <10; I ++) {printf ("% d", arr [I]);} system ("pause"); return 0 ;}

```

Running result:

`Before sort: 41 67 34 0 69 24 78 58 62 64 After sort: 0 24 34 41 58 62 64 67 69 78 press any key to continue...`

In case of any errors, please do not hesitate to point out.

Related Keywords:
Related Article

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.