C # algorithm-merge and sort

Source: Internet
Author: User

At noon today, I am free to help you sort and combineAlgorithm(Recursive C # Version)

Let's take a look at the basic strategy of the next merge and sort-balancing and simple binary division:

• The columns to be sorted are simply divided into left columns of roughly equal sizes.

• The right two segments, and then recursively add the two sub-Columns

• Merge and sort rows, and use these two sub-columns to obtain

• Ordering, merge them in an orderly manner in a work zone, and finally use

• Update the original element to be sorted by sorting all columns in the work area

• Columns are required for sorting element columns.

The format of the token is as follows:

Int [] Ar =   { 1 , 4 , 34 , 8 , 78 , 4 , 5 , 6 , 9 , 34 , 65 , 78 , 9 , 12 } ;

This . Mergesoft ( Ref Ar, 0 , Ar. Length - 1 );

For ( Int I = 0 ; I < Ar. length; I ++ )

{

Ar [I];

}

The Zookeeper program is as follows:
Private   Void Mergesoft ( Ref   Int [] ARRA, Int Left, Int Right)

{

If (Left < Right)

{

Int Mid = (Left + Right) / 2 ;

Mergesoft ( Ref ARRA, left, mid );

Mergesoft ( Ref ARRA, mid + 1 , Right );

Int [] ARRC = Merge (ARRA, left, mid, right );

Copyarray ( Ref ARRA, ARRC, left, right );

}

}

Sort the Left and Right Parts
Private   Int [] Merge ( Int [] ARRA, Int Left, Int Mid, Int Right)

{

Int [] Arrb =   New   Int [Right - Left + 1 ];

Int I = Left, J = Mid + 1 , K = 0 ;

While (I <= Mid) && J <= Right)

If (ARRA [I] <= ARRA [J]) arrb [K ++ ] = ARRA [I ++ ];

Else Arrb [K ++ ] = ARRA [J ++ ];

If (I > Mid && J <= Right)

For ( Int Q = J; q <= Right; q ++ )

Arrb [K ++ ] = ARRA [Q];

If (J > Right && I <= Mid)

For ( Int Q = I; q <= Mid; q ++ )

Arrb [K ++ ] = ARRA [Q];

Return Arrb;

}

Merge sorted data into the original data group
Private   Void Copyarray ( Ref   Int [] ARRA, Int [] ARRC, Int Left, Int Right)

{

For ( Int I = Left, J = 0 ; I <= Right && J < ARRC. length; I ++ , J ++ )

ARRA [I] = ARRC [J];

}

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.