Fast sorting (C # Data Structure Learning 10)

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. text;

Namespace Solodatastructure
{
Class Myquicksort
{
/**/ ///   <Summary>
/// Quick Sorting Algorithm
///   </Summary>
/// The fast sorting method is unstable, and the time complexity O (nlog2n) is the fastest sorting method of the same order of magnitude.
///   <Param name = "arr"> Partitioned Array </Param>
///   <Param name = "low"> Low-end array tagging </Param>
///   <Param name = "high"> Array High-end subscript </Param>
///   <Returns> </returns>

Static   Int Partition ( Int [] Arr, Int Low, Int High)
{
// Perform a quick sorting and return the location of the central axis record
// Arr [0] = arr [low];
Int Bytes = Arr [low]; // Place the central axis in arr [0]
While (Low < High)
{
While (Low < High && Arr [High] > = Token)
-- High;
// Move a record smaller than the central axis to the lower end
Swap ( Ref Arr [High], Ref Arr [low]);
While (Low < High && Arr [low] <= Token)
++ Low;
Swap (
Ref Arr [High], Ref Arr [low]);
// Move larger than the central axis record to high-end
}

Arr [low]
= Break; // Move the center axis to the correct position
Return Low; // Returns the center axis position.
}

Static   Void Swap ( Ref   Int I, Ref   Int J)
{
IntT;
T
=I;
I
=J;
J
=T;
}
 
Static   Void Quicksort ( Int [] Arr, Int Low, Int High)
{
If (Low < High - 1 ) // When arr [low, high] is null or only one record does not need to be sorted
{< br> int trim = partition (ARR, low, high);
quicksort (ARR, low, margin
- 1 );
quicksort (ARR, margin
+ 1 , high);

}

}

Static   Void Main ( String [] ARGs)
{
Int [] Arr = New   Int [] { 54 , 62 , 99 , 14 , 28 , 1 , 8 , 77 , 99 , 3 , 110 } ;
Quicksort (ARR,
0 , Arr. Length - 1 );
Console. Write (
" Data after quicksort: " );
Foreach ( Int I In ARR)
{
Console. Write (I
+",");
}

Console. Readline ();
}

}

}

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.