One-step write algorithm (merging and sorting)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

The quick sorting mentioned in the previous blog is a classic algorithm in the sorting algorithm. As with quick sorting, Merge Sorting is another sort algorithm that is frequently used. So what are the differences between the merge sorting algorithms? The key lies in the merging.

The basic steps of the merge algorithm are as follows:

1) set 0 ~ The length-1 array is divided into the left array and the right array.

2) iteratively sorts the Left and Right Arrays

3) Merge the left array and right array, and the generated entire array is an ordered data array.

 

 

Next we will start to practice:

A) create a function to determine the validity of the Parameter

 

 

 

Void merge_sort (int array [], int length)

{

If (NULL = array | 0 = length)

Return;

_ Merge_sort (array, 0, length-1 );

}

Void merge_sort (int array [], int length)

{

If (NULL = array | 0 = length)

Return;

_ Merge_sort (array, 0, length-1 );

}

B) Performing merge Function Iteration

 

 

 

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 );

}

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 queues.

 

 

 

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] <= 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 ));

 

If (right <= end)

Memmove (& pData [all], & array [right], sizeof (int) * (end-right + 1 ));

Memmove (& array [start], pData, sizeof (int) * length );

Free (pData );

}

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] <= 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 ));

 

If (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 article is not an optimal solution. It can be replaced by other data types in actual development.

 

 

 

 

D) Write 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 = array [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]);

}

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 = array [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 similarities and differences between quick sorting and Merge Sorting:

Similarities: iterative operations

Differences: Fast sorting, first classification and then iteration; Merge Sorting, first iteration and then merge

Related Article

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.