Anzahl der Swaps in Bubble Sort
Ich habe eine Version von Bubble Art:
int i, j;
for i from n downto 1
{
for j from 1 to i-1
{
if (A[j] > A[j+1])
swap(A[j], A[j+1])
}
}
Ich möchte die erwartete Anzahl von Swaps mit der obigen Version der Blasensortierung berechnen. Die von mir verwendete Methode ist unten gezeigt:
// 0 based index
float ans = 0.0;
for ( int i = 0; i < n-1; i++ )
{
for ( int j = i+1; j < n; j++ ) {
ans += getprob( a[i], a[j]); // computes probability that a[i]>a[j].
}
}
Gehe ich in die richtige Richtung oder fehle ich etwas?