Quick sorting of classic algorithms

Source: Internet
Author: User

Note:

Quick sorting is currently one of the fastest sorting methods. In most cases, the performance is good, but when the worst case must be considered, the fast sorting is unacceptable because the time reaches O (n2 ).

 

Ideas:

The basic idea of quick sorting is to find an appropriate axis in a series and divide the series into two parts (the elements in the left half are smaller than those in the right half ), then sort the two parts separately (re-select the axis and divide the left and right parts ).

Steps for processing with an intermediate element as the axis (expressed in P:

1. Search index I from left to right until the number greater than P is found.

2. Search index J from right to left until the number less than P is found.

3. If I> = J, skip to Step 5.

4. Otherwise, the values of the elements I and J are exchanged.

5. Perform the steps on the left.

6. perform the following steps on the right.

 

Note: The choice of the axis has an impact on the efficiency of quick sorting, but the center element is the axisAlgorithmEasy to understand.

 

CoreCode:

Code

// Take the intermediate element as the axis
Private   Static   Void Sort2 ( Int [] Array, Int Left, Int Right ){
If (Left < Right ){
Int P = Array [(left + Right) /   2 ];
Int I = Left -   1 ;
Int J = Right +   1 ;
While ( True ){
While (Array [ ++ I] < P)
;
While (Array [ -- J] > P)
;
If (I > = J)
Break ;
Swap (array, I, j );
}
Sort (array, left, I -   1 );
Sort (array, J +   1 , Right );
}
}

 

 

All code:

Code

Package Com. icescut. Classic. algorithm. Sort;

Public ClassQuicksort {

Public   Static   Void Main (string [] ARGs ){
Int [] Array = { 10 , - 3 , 5 , 34 , - 34 , 5 , 0 , 9 }; // Test Data
Sort2 (array, 0 , Array. Length -   1 );
For ( Int El: array ){
System. Out. Print (El +   "   " );
}
}

// Bearing element on the left
Private   Static   Void Sort ( Int [] Array, Int Left, Int Right ){
If (Left < Right ){
Int P = Array [left];
Int I = Left;
Int J = Right +   1 ;
While ( True ){
While (I +   1   < Array. Length && Array [ ++ I] < P)
;
While (J -   1   >   - 1   && Array [ -- J] > P)
;
If (I > = J)
Break ;
Swap (array, I, j );
}
Array [left] = Array [J];
Array [J] = P;
Sort (array, left, J -   1 );
Sort (array, J +   1 , Right );
}
}

// Take the intermediate element as the axis
Private   Static   Void Sort2 ( Int [] Array, Int Left, Int Right ){
If (Left < Right ){
Int P = Array [(left + Right) /   2 ];
Int I = Left -   1 ;
Int J = Right +   1 ;
While ( True ){
While (Array [ ++ I] < P)
;
While (Array [ -- J] > P)
;
If (I > = J)
Break ;
Swap (array, I, j );
}
Sort (array, left, I -   1 );
Sort (array, J +   1 , Right );
}
}

Private   Static   Void Swap ( Int [] Number, Int I, Int J ){
Int T = Number [I];
Number [I] = Number [J];
Number [J] = T;
}
}

 

 

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.