Quickly sort the desired places

Source: Internet
Author: User

Merging and sorting is completed. Now we can sort it quickly.

Simply put, quick sorting

Quick sorting is to set a set, an intermediate element, and then divide the set into three sets.

The subset of the left set is less than or equal to the intermediate element ---- the right subset is greater than or equal to the intermediate element

 

It can be solved by recursive calling,

Below is a simple and fast sorting, and there are many variant versions. Please refer to the relevant materials. Here is the original code.

//////////////////////////////////////// ///////////////// //////////////////////////////////////// //////////////////

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace fastsort
{
Class Program
{
Static void main (string [] ARGs)
{
Int [] arraytest = new int [] {, 5 };
Quicksort (arraytest,); // recursive call
Foreach (INT test in arraytest)
{
Console. Write (test. tostring () + "");
}
Console. Read ();
}

Static int partition (INT [] test, int P, int R) // separate the Core Algorithm for Bidirectional Scanning
{
Int I = P;
Int J = R + 1;

Int x = test [p];

While (true)
{
While (test [-- J]> X)
{

}
While (test [++ I] <X)
{

}
If (I <j)
{
Swap (test, I, j );
}
Else
{
Swap (test, P, J );
Return J;
}
}
}

Static void quicksort (INT [] test, int L, int R)
{
If (L> = r) // If the left and right are equal
{
Return;
}
Int M = partition (test, L, R );
Quicksort (test, L, M );
Quicksort (test, m + 1, R );


}

Static void swap (INT [] test, int X, int y)
{
If (x = y)
{
Return;
}
Int temp = test [x];
Test [x] = test [y];
Test [y] = temp;
}
}
}
S

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.