Select sorting algorithm efficiency

Sorting algorithm can solve the problem more than a selection sort. So, when there are multiple algorithms can solve the same problem, how do we choose?

In the judgment algorithm, taking into consideration the standards are different.

For example, a simple algorithm is easy to understand for humans, can also be easily written procedures, and in the process running the algorithm does not need to spend too much space resources, it is very suitable for small computer memory.
However, in general, most of our attention is the running time of the algorithm, that is, the time from the input data to the output result of this process takes.

Use whole ranking algorithm sort
order for us to understand what effect the low efficiency of the algorithm, here take a look at the following sorting algorithm.
① generates a series consisting of n number (not the number of columns and the previously generated repeats)
② ① generated if the number of columns in ascending order of their output will otherwise returns to step ①

This algorithm is called "full ranking algorithm."

Full ranking algorithm lists all the arrangement method, so regardless of the input, you can get the right result.


So you have to wait long before the result? If you are lucky, will soon appear correctly aligned, the result will come out immediately. However, the reality is often not as we wished.

In the worst case situation, that is, until the last to appear aligned correctly, the computer will have to confirm all the possible permutations.
n digital arrangement has n different methods! (n = n (n - ! 1) (n - 2) ... 3 · 2 · 1).

Now, when we look at n = 50 What is it like now.

① 50! =50·49·48…3·2·1
② 50·49·48…3·2·1> 50·49·48…13·12·11
③ 50·49·48…13·12·11 >1040

① In the formula, 50! Is the numbers 1 to 50 of the digital product. For ease of calculation, the result is approximately ②③ we convert the form of the power of n using Equation 10.

The right part of equation ② removed numbers below 10, thus less than 50 !.

The product of the formula ③ is about 40 digits, but left number greater than 10, greater than 10 and therefore the right 40 .

Then we 10 40 approximate arrangement of representing all 50 digits to be calculated.
Suppose a high-performance computer can check 1000000000000 1 second (= 10 12 is ) the number of columns, check the top 10 40 times the number of columns will take 10 40 ÷ 10 12 is = 1028 seconds. 1 year 31 536 000 seconds and less than 10 8 seconds. Thus 10 28 seconds of> 10 20 is years.
From the beginning of the Big Bang universe has gone through about 13.7 billion years, even that less than 10 11 years. In other words, just 50 digits are sorted, the use of full permutation algorithm, even if it takes 109 times longer than the age of the universe they have no answer.

 

Using a selection sorting algorithm to sort
it, using the previously mentioned selection sort algorithm, the case, how will it?
First, in order to find the smallest number in the first round, we need to confirm the figures the number of columns from left to right, as long inquiry n digits can be. In the next second round, the need to n - 1 numbers to find the minimum, it is need, n - 1 number digits.
When this step is carried out to the n-th wheel, the number of need, is as follows.
+ n-(N-. 1) + (N- 2). 3 + ... + n-2+. 1 = (n-+. 1) / 2 ≦ n- 2
n-= 50 when n- 2 = 2500. Suppose one second can confirm 1000000000000 (= 10 12 is ) digits, then ÷ 10 2500 12 is = 0.000 000 002 the outcome. 5 seconds or so, is much higher than the efficiency of the total of the ranking algorithm.

Guess you like

Origin www.cnblogs.com/hlaotong/p/12661562.html