| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Permutation: Get number of inversions</title> |
| 3 | <style> |
| 4 | td { padding-left: 5pt; padding-right: 5pt; } |
| 5 | th { padding-left: 5pt; padding-right: 5pt; } |
| 6 | code { white-space: pre-wrap; } |
| 7 | dd { white-space: pre-wrap; } |
| 8 | </style> |
| 9 | </head><body bgcolor="#FFFFFF"> |
| 10 | |
| 11 | <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 | Permutation: Get number of inversions |
| 13 | </b></font></table></table> |
| 14 | <p>Get the number of <i>inversions</i> in a Permutation object. An inversion is defined as: if i < j then p(i) > p(j).</p> |
| 15 | <h2>Examples</h2> |
| 16 | <p>The query for the number of inversions in the permutation (3,2,4,5,1) gives 3 because 3>2, 3>1 and 2>1.</p> |
| 17 | <p>The permutation (1,2,3,4) has 0 inversions.</p> |
| 18 | <p>The permutation (4,3,2,1) has 6 inversions.</p> |
| 19 | <p>The number of inversions of a permutation with <i>n</i> elements is always in the closed interval [0, <i>n</i>(<i>n</i>-1)/2].</p> |
| 20 | <h3>Links to this page</h3> |
| 21 | <ul> |
| 22 | <li><a href="Permutation.html">Permutation</a> |
| 23 | </ul> |
| 24 | <hr> |
| 25 | <address> |
| 26 | <p>© djmw 20250120</p> |
| 27 | </address> |
| 28 | </body> |
| 29 | </html> |