Data Structure Learning (C ++) continued-Sort [4] Select sort

Source: Internet
Author: User
[4] Select sorting

The basic idea is: each time a record with a small I value is selected, it is placed at the position I (the starting point of I is 0. According to this statement, a 0th small record is actually the smallest, a bit awkward, no matter how much ). It's done when I = N-1.

Select sort directly

Direct selection and sorting simply reproduce the basic idea of selection and sorting. The first time we look for the smallest element, the cost is O (n). If we do not do some special processing, we use the simplest method of searching each time, naturally, the time complexity of the entire sorting is O (n2.

Template <class T>

Void selectsort (t a [], int N, Int & kcn, Int & rmn)

{

Kcn = 0; rmn = 0;

For (INT I = 0; I <n; I ++)

{

For (Int J = I + 1, K = I; j <n; j ++) if (++ kcn & A [J] <A [k]) k = J; // select Min

If (K! = I) {swap (A [K], a [I]); rmn + = 3 ;}

}

}

Test results:

Sort Ascending n = 10000 timespared:721 Ms

Kcn = 49995000 kcn/N = 4999.5Kcn/n ^ 2 = 0.49995Kcn/nlogn = 376.25

Rmn = 0 rmn/n = 0 rmn/n ^ 2 = 0 rmn/nlogn = 0

Sort randomness n = 10000 timespared:711 Ms

Kcn = 49995000 kcn/N = 4999.5Kcn/n ^ 2 = 0.49995Kcn/nlogn = 376.25

Rmn = 29955 rmn/N = 2.9955 rmn/n ^ 2 = 0.00029955 rmn/nlogn = 0.225434

Sort Descending n = 10000 timespared:711 Ms

Kcn = 49995000 kcn/N = 4999.5Kcn/n ^ 2 = 0.49995Kcn/nlogn = 376.25

Rmn = 15000 rmn/N = 1.5 rmn/n ^ 2 = 0.00015 rmn/nlogn = 0.112886

We can see that kcn is fixed to n (n-1)/2. Another interesting thing is that the forward order of rmn = 0 takes more time than the disordered order of rmn close to 3 (n-1. First, it indicates that the test accuracy is not enough. It is common that the test results fluctuate by 10 ms on my machine. Second, it is compared with the N (n-1)/2 of kcn, the 3 (n-1) of rmn is insignificant.

Title sorting

From the perspective of direct sorting, the bottleneck of the algorithm lies in kcn. In fact, the time complexity can be reduced to O (logn) for the follow-up to find the minimum value ). The most direct approach is to adopt the championship approach. After the champion is taken away, as long as the champion is replayed in the competition, then the "champion" of the rest will be created, the number of rematches is the depth of the Competition tree. In actual writing, it will be "stupid" if it is difficult to write, not only occupying a large amount of memory, but also leading to useless judgment. I have never seen a satisfactory routine (the Yin version is really disgusting). I can't write anything beautiful, so I won't show it ugly (in fact, this is due to inertia, with quick sorting, most people will not be interested in other internal columns except for base sorting ). When you are bored, you may wish to write (or improve) the final order to pass the time, ^ _ ^.

Heap sorting

There is too much additional storage for the final sorting, and efficient search for the maximum or minimum values (O (logn), we also have a way to heap. The largest heap is used here, and the space to be recorded is used as the heap space. The record at the top of the heap (currently the largest) is exchanged with the last record of the heap, when the heap is gradually reduced to 1, the records are sorted. Obviously, the time complexity is O (nlogn), and there is no bad situation.

Template <class T>

Void filterdown (t a [], int I, int N, Int & kcn, Int & rmn)

{

Int child = 2 * I + 1; t temp = A [I];

While (Child <n)

{

If (Child <n-1 & A [child] <A [Child + 1]) Child ++;

If (++ kcn & temp> = A [child]) break; // No need to adjust, end Adjustment

A [I] = A [child]; rmn ++;

I = Child; child = 2 * I + 1;

}

A [I] = temp; rmn ++;

}

Template <class T>

Void heapsort (t a [], int N, Int & kcn, Int & rmn)

{

Int I;

For (I = (n-2)/2; I> = 0; I --) filterdown <t> (A, I, n, kcn, rmn ); // generate the maximum heap

For (I = n-1; I> 0; I --)

{

Swap (A [0], a [I]); rmn + = 3;

Filterdown (A, 0, I, kcn, rmn );

}

}

Test results: N = 100000:

Sort Ascending n = 100000 timespared: 110 ms

Kcn = 1556441 kcn/N = 15.5644 kcn/n ^ 2 = 0.000155644kcn/nlogn = 0.937071

Rmn = 2000851 rmn/N = 20.0085 rmn/n ^ 2 = 0.000200085rmn/nlogn = 1.20463

Sort randomness n = 100000 timespared:160 Ms

Kcn = 3047006Kcn/N = 30.4701 kcn/n ^ 2 = 0.000304701kcn/nlogn = 1.83448

Rmn= 3898565Rmn/N = 38.9857 rmn/n ^ 2 = 0.000389857rmn/nlogn = 2.34717

Sort Descending n = 100000 timespared:90 ms

Kcn = 4510383Kcn/N = 45.1038 kcn/n ^ 2 = 0.000451038kcn/nlogn = 2.71552

Rmn= 5745996Rmn/N = 57.46 rmn/n ^ 2 = 0.0005746 rmn/nlogn = 3.45943

The overall performance is very good. The additional storage 1 is not very bad. If you are not at ease with the worst case of fast sorting, heap sorting is indeed a good choice. Here, there are still examples of kcn and rmn which consume less time. The error 70 ms is impossible. It seems that the role of CPU optimization is very obvious (possibly related to cache ).

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.