Step-by-step write algorithm (merge sort)

Source: Internet
Author: User

Original: Step by step Write algorithm (merge sort)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


The quick sort mentioned in the previous blog post is a classical algorithm in the sorting algorithm. As with quick sort, merge sort is another sort of frequently used sorting algorithm. So what's the difference between a merge sort algorithm? The key is reflected in the merger.
The basic steps of the merge algorithm are as follows:
1) Divide the 0~length-1 array into the left and right arrays
2) sort the left and right arrays by iteration
3) merging the left and right arrays, the resulting array is an ordered array of data


Here's how to get started:
A) Create a function to determine the legitimacy of the parameter

void Merge_sort (int array[], int length) {if (NULL = = Array | | 0 = = length) return; _merge_sort (array, 0, length-1);}

b) Iterative operation of the merge function

void _merge_sort (int array[], int start, int end) {if (Start >= end) return;int middle = start + ((End-start) >> 1 ); _merge_sort (array, start, middle), _merge_sort (array, middle + 1, end); _merge_data_in_array (array, start, middle, end) ;}

c) Merge the merged queue

void _merge_data_in_array (int array[], int start, int middle, int end) {int length = End-start + 1;int* PData = Null;int left = Start;int right = middle + 1;int all = 0;/* Allocate new memory to the space */pdata = (int*) malloc (sizeof (int) * length); assert (NULL! = pData); memset (pData, 0, length);/* Begin to move data */while (right <= end) {while (Array[left] &l t;= Array[right] && left <= middle) {Pdata[all] = Array[left]; left + +; all + +;} if (Left > middle)  {break;} while (Array[left] > Array[right] && right <= end) {Pdata[all] = Array[right], right + +; all + +;}} /* Move the left data */if (left <= middle) memmove (&pdata[all], &array[left], sizeof (int) * (Middle-left + 1)); F (right <= end) Memmove (&pdata[all], &array[right], sizeof (int) * (end-right + 1)); Memmove (&array[start) , PData, sizeof (int) * length); free (pData);}
Note: The pdata dynamic memory used in this paper is not an optimal processing method and can be replaced by other forms of data type in actual development.


D) Writing test cases

static void Test1 () {int array[] = {1};merge_sort (array, sizeof (array)/sizeof (int));} static void Test2 () {int array[] = {2, 1};merge_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), assert (2 = = A RRAY[1]);} static void Test3 () {int array[] = {3, 2, 1};merge_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), assert (2 = = Array[1]); assert (3 = = array[2]);} static void Test4 () {int array[] = {4, 3, 5, 1};merge_sort (array, sizeof (array)/sizeof (int)), assert (1 = = Array[0]), ASSERT ( 3 = = Array[1]), assert (4 = = array[2]), assert (5 = = array[3]);}

Analyze the same and different points for quick sort and merge sorting:
The same point: all iterative operations
Different points: fast sorting, first sorting and then iteration, merging sorting, first iteration and merging

"trailer: The following blog mainly describes the heap sort"


Step-by-step write algorithm (merge sort)

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.