Introduction to Reading Notes algorithms quick sorting quicksort uses the last element as the marker

Source: Internet
Author: User

Quick sorting is the most common sorting method in practical programming applications.

He has excellent performance.

The worst case time complexity is O (n square meters)

The average time complexity is O (n logn) and has a very small coefficient.

And the space complexity is very small, that is, O (n)

But thisAlgorithmIt is also difficult to understand...

The following is a quick algorithm that uses the last element as the token.

 Using  System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. runtime. interopservices;

Namespace Introducetoalgorithm
{

Public Class Quicksort
{
Public Void Sort ( Int [], Int Startindex = 1 , Int Endindex = - 1 )
{
If (Endindex = - 1 )
{
Endindex = A. length;
}
If (Startindex < Endindex)
{
Int Q = Partition (A, startindex, endindex );

Sort (A, startindex, Q - 1 );
Sort (A, Q + 1 , Endindex );
}
}

Public Int Partition ( Int [], Int Startindex, Int Endindex)
{
Int X = A [endindex - 1 ];
Int I = Startindex - 1 ;

For ( Int J = Startindex; j <= Endindex - 1 ; J ++ )
{
If (A [J - 1 ] <= X)
{
I = I + 1 ;
Exchange (A, I - 1 , J - 1 );
}
// A. Print ();
}
Exchange (A, I, endindex - 1 );
// A. Print ();
Return I + 1 ;


}

Public Void Exchange ( Int [], Int I, Int I2)
{
Int Val = A [I];
A [I] = A [I2];
A [I2] = Val;
}
}

}

PS: this algorithm is hard to understand when it is implemented...CodeSee... although there are few codes

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.