C # Quick sorting

Source: Internet
Author: User

Quick sorting:

An Improvement on Bubble sorting based on the grouping policy. For a sequence to be sorted, sort the selected value and place it to the correct position. Then, the position field is used to sort the Left and Right Parts. Until the partition length is 1.


Step: Set a sequence to be sorted.

1. Set low and high to the leftmost and rightmost ends of the sequence respectively. Select one from the sequence for sorting (usually select the leftmost value low to the value) and store it to tmp;
2. Starting from the high end, search for something smaller than tmp. After finding it, place the value in the storage space to which low points. At the same time, point high to the location of the value currently found;
3. Start from the low end and find something bigger than tmp. After finding it, place the value to the storage class pointing to high, and low point to the location of the value currently found;
4. If the low bit is smaller than the high bit, step 2 is returned; otherwise, the tmp value is saved to the empty position pointed to by low + 1 and exited. The position of low is returned;
5. Divide the sequence into two parts based on position and sort the two parts respectively.


C # implementation:

// Quick sorting
Public static void QuickSort (int [] items)
{
RecQuickSort (items, 0, items. Length-1 );
}

Private static void RecQuickSort (int [] items, int low, int high)
{
If (low {
Int I = Partition (items, low, high );
RecQuickSort (items, low, I-1 );
RecQuickSort (items, I + 1, high );
}
}

Private static int Partition (int [] items, int low, int high)
{
Int tmp = items [low];
While (low {
While (low High --;

// Do not add 1 to low after transposition to prevent skip
If (low Items [low] = items [high];

While (low Low ++;

If (low {
Items [high] = items [low];
// With low High --;
}
}
Items [low] = tmp;

Return low;
}

The most important thing is Partition. Make a sorting division and place it in the correct position.


In. NET, the Array. Sort () method uses a fast sorting algorithm. Let's look at the implementation of Array. Sort:

[ReliabilityContract (Consistency. MayCorruptInstance, Cer. MayFail)]
Public static void Sort (Array array)
{
If (array = null)
{
Throw new ArgumentNullException ("array ");
}
Sort (array, null, array. GetLowerBound (0), array. Length, null );
}

[ReliabilityContract (Consistency. MayCorruptInstance, Cer. MayFail)]
Public static void Sort (Array keys, Array items, int index, int length, IComparer comparer)
{
If (keys = null)
{
Throw new ArgumentNullException ("keys ");
}
If (keys. Rank! = 1) | (items! = Null) & (items. Rank! = 1 )))
{
Throw new RankException (Environment. GetResourceString ("Rank_MultiDimNotSupported "));
}
If (items! = Null) & (keys. GetLowerBound (0 )! = Items. GetLowerBound (0 )))
{
Throw new ArgumentException (Environment. GetResourceString ("Arg_LowerBoundsMustMatch "));
}
If (index <keys. GetLowerBound (0) | (length <0 ))
{
Throw new ArgumentOutOfRangeException (length <0 )? "Length": "index", Environment. GetResourceString ("ArgumentOutOfRange_NeedNonNegNum "));
}
If (keys. Length-(index-keys. GetLowerBound (0) <length) | (items! = Null) & (index-items. GetLowerBound (0)> (items. Length-length ))))
{
Throw new ArgumentException (Environment. GetResourceString ("Argument_InvalidOffLen "));
}
If (length> 1) & (comparer! = Comparer. Default) & (comparer! = Null) |! TrySZSort (keys, items, index, (index + length)-1 )))
{
Object [] objArray =

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.