Arithmetic quick sorting (improved bubble)

Source: Internet
Author: User
1,

Quick sorting uses the divide and conquer policy to divide a serial (list) into two sub-serial (sub-lists ).

Steps:

    1. Picking an element from a series is called a benchmark ),
    2. Re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After the partition exits, the benchmark is in the middle of the series. This is calledPartition)Operation.
    3. Recursively (recursive) sorts the subseries smaller than the reference value element and the subseries larger than the reference value element.

The bottom of recursion is that the number of columns is zero or one, that is, they are always sorted. Although it has been recursive, but thisAlgorithmIt always exits, because in each iteration, it will at least place an element at its final position.

C #CodeAs follows:

View code 1 Public   Static   Void Sort ( Int [] Numbers)
2 {
3 Sort (numbers, 0 , Numbers. Length -   1 );
4 }
5  
6 Private   Static   Void Sort ( Int [] Numbers, Int Left, Int Right)
7 {
8 If (Left < Right)
9 {
10 Int Middle = Numbers [(left + Right) /   2 ];
11 Int I = Left -   1 ;
12 Int J = Right +   1 ;
13 While ( True )
14 {
15 While (Numbers [ ++ I] < Middle );
16  
17 While (Numbers [ -- J] > Middle );
18  
19 If (I > = J)
20 Break ;
21  
22 Swap (numbers, I, j );
23 }
24  
25 Sort (numbers, left, I -   1 );
26 Sort (numbers, J +   1 , Right );
27 }
28 }
29  
30 Private   Static   Void Swap ( Int [] Numbers, Int I, Int J)
31 {
32 Int Number = Numbers [I];
33 Numbers [I] = Numbers [J];
34 Numbers [J] = Number;
35 }

Reference: algorithm Summary

 

 

 

 

 

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.