According to the merge sort principle, its own implementation of the merge sorting algorithm + detailed annotation + code

Source: Internet
Author: User
Tags foreach array sort sort

1. Not much nonsense, I have written the comments in detail, the C # Implementation of the sharing is as follows:

<summary>
Merge sort: Merge sort entry
Updated by Lihua at 05/06/2009
</summary>
<param name= "Data" > unordered array </param>
<returns> ordered Array </returns>
<author>lihua</author>
<copyright>www.zivsoft.com</copyright>
Int[] Sort (int[] data)
{
If data is null, or only 1 or 0 elements are left, return, not sorted
if (null = Data | | data. Length <= 1)
{
return data;
}

Take the middle subscript of an array
int middle = data. LENGTH/2; Method one: Except 2 to take the whole number
int middle = data.  Length >> 1; Method Two: Displacement (thanks to the reader hyper given this slightly more efficient method)

Initializes the temporary array let,right and defines result as the final ordered array, and if the array element is odd, the extra element space is reserved on the right temporary array
Int[] left = new Int[middle], right = new int[data. Length-middle], result = new Int[data. Length];
           
The following sentence has some effect on performance, so there is an improvement on it, directly using data. Length-middle Initialize right array
if (data. Length% 2!= 0) right = new Int[middle + 1];

int i = 0, j = 0;
foreach (int x in data)/start sort
//{
if (I < middle)//fill left array
{
Left[i] = x;
i++;
}
else//padding Right Array
{
RIGHT[J] = x;
j + +;
}
//}

The foreach above was changed to a for loop
for (int i = 0; i < data. Length; i++)
{
if (I < middle)//with middle, not left. Length, will not be better, hehe, who knows?
{
Left[i] = Data[i];
}
Else
{
Right[i-middle] = Data[i]; Here I-middle, let me dispense with the definition of a J, improve performance
}
}

Ieft = Sort (left);//recursive left-hand array
OK = Sort (right);//recursive right-hand array
result = Merge (left, right);/start sort
This. Write (result);//output sort, test (Lihua Debug)
return result;
}
<summary>
Merge sort of and: sort in this step
</summary>
<param name= "A" > Left array </param>
<param name= "B" > right array </param>
<returns> Merge the left and right array sort and return to </returns>
Int[] Merge (int[] A, int[] b)
{
Defines an array of results to store the final result
Int[] result = new Int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (I < a.length && J < B.length)
{
if (A[i] < B[J])//The element in the left array is less than the element in the right array
{
result[k++] = a[i++];//Place the small one in the result array
}
else//the element in the left array is greater than the element in the right array
{
result[k++] = b[j++];//Place the small one in the result array
}
}
while (I < a.length)//Here is actually the left element, but there is no right element
{
result[k++] = a[i++];
}
while (J < b.length)/Right right element, no left element
{
result[k++] = b[j++];
}
Return result;//returns an array of results
}

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.