algorithm - selection sort running time confusion -


"Uses sorting sort ~ N 2 / 2 and compares exchanges Length of time to sort an array. "" For example, I have two items in the array. String [] a = {"h", "t"};
Do I think that the selection compares sort 2 2 / 2 = 2, and to sort an array of 2 exchanges length 2?
But when I run it: it only compares once. Of course this is common knowledge because only the items to compare are H and T but I am still confused with the statement, is there any mistake in my experiment ? I'm new to this.

itemprop = "text">

The choice almost uses n ^ 2/2 comparison and n exchanges.

For precise analysis, the selection type uses

N-1 + N-2 + N-3 + ... to sort an array of length For 1 comparison. / P>

Thus, the total comparison = (N-1) * (N) / 2 = N ^ 2/2 - N / 2, which is equivalent to N ^ 2/2, who has written it.

In your example when N2 is, compare = 1 * 2/2 = 1. and the number of exchanges = 1. (N-1)


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -