[Algorithm] C # Quick sorting class

Source: Internet
Author: User

The basic idea of quick sorting is based on the divide and conquer policy. If the input sub-sequence AP. Ar is small enough, the sub-sequence is sorted directly. Otherwise, the sub-sequence is processed in three steps:

Divide ):Set the input sequence AP .. ar is divided into two non-empty subsequence APS .. AQ and AQ + 1 .. ar To make the ap .. the value of any element in AQ is not greater than AQ + 1 .. the value of any element in Ar.

Recursive solution (conquer ):Recursively sorts P. aq and AQ + 1. ar.

Merge (merge ):Because the sorting of the two sub-sequences obtained by splitting is performed in place .. AQ and AQ + 1 .. after the AR is sorted, no computing AP is required .. ar is sorted.

This solution process is in line with the division and Control Law. Therefore, quick sorting is one of the classic application instances of the divide and conquer method.

Using system;

Namespace vcquicksort

{

/// <Summary>

/// Classquicksort quick sorting.

/// </Summary>

Public class quicksort

{

Public quicksort ()

{

}

Private void swap (ref int I, ref Int J)

// Swap two integer

{

Int T;

T = I;

I = J;

J = T;

}



Public void sort (INT [] list, int low, int high)

{

If (high <= low)

{

// Only one element in array list

// So it do not need sort

Return;

}

Else if (high = low + 1)

{

// Means two elements in array list

// So we just compare them

If (list [low]> list [High])

{

// Exchange them

Swap (Ref List [low], Ref List [High]);

Return;

}

}

// More than 3 elements in the arrary list

// Begin quicksort

Myquicksort (list, low, high );

}

Public void myquicksort (INT [] list, int low, int high)

{

If (low
{

Int partition = partition (list, low, high );

Myquicksort (list, low, skip-1 );

Myquicksort (list, tables + 1, high );

}

}

Private int partition (INT [] list, int low, int high)

{

// Get the attributes of the arrary list

Int timeout;

Dependencies = list [low];

While (low
{

While (low <High & list [High]> = running)

{

High --;

}

If (low! = High)

{

Swap (Ref List [low], Ref List [High]);

Low ++;

}

While (low <High & list [low] <= lower)

{

Low ++;

}

If (low! = High)

{

Swap (Ref List [low], Ref List [High]);

High --;

}

}

Return low;

}

}

}

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.