Fast Sorting Algorithm

Source: Internet
Author: User

Fast Sorting Algorithm

// Quicksort implementation
Static void quicksort (arraylist szarray, int nlower, int nupper)
{
// Check for non-base case
If (nlower <nupper)
{
// Split and sort partitions
Int nsplit = partition (szarray, nlower, nupper );
Quicksort (szarray, nlower, nsplit-1 );
Quicksort (szarray, nsplit + 1, nupper );
}
}
// Quicksort partition implementation
Static int partition (arraylist szarray, int nlower, int nupper)
{
// Starts with first element
Int nleft = nlower + 1;
String szwer = (string) szarray [nlower];
Int nright = nupper;
// Partition array elements
String szswap;
While (nleft <= nright)
{
// Find item out of place
While (nleft <= nright)
{
If (string) szarray [nleft]). compareto (szeto)> 0)
Break;
Nleft = nleft + 1;
}
While (nleft <= nright)
{
If (string) szarray [nright]). compareto (szeto) <= 0)
Break;
Nright = nright-1;
}
// Swap values if necessary
If (nleft <nright)
{
Szswap = (string) szarray [nleft];
Szarray [nleft] = szarray [nright];
Szarray [nright] = szswap;
Nleft = nleft + 1;
Nright = nright-1;
}
}
// Move another element
Szswap = (string) szarray [nlower];
Szarray [nlower] = szarray [nright];
Szarray [nright] = szswap;
Return nright;
}

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.