Print Inversions pairs in array in php
Php program for Print Inversions pairs in array. Here more solutions.
<?php
/*
Php program for
Display inversions pairs in array
*/
class InversionPairs
{
// Print the all inversion pairs in given array
public function printInversionPair($arr, $size)
{
if ($size <= 1)
{
return;
}
// Outer loop
for ($i = 0; $i < $size; ++$i)
{
// Inner loop
for ($j = $i + 1; $j < $size; ++$j)
{
if ($arr[$i] > $arr[$j])
{
// When element of i is
// greater than element of j
// Print inversion pair
echo "(".($arr[$i]).
",".($arr[$j]).
")\n";
}
}
}
}
public static function main()
{
$task = new InversionPairs();
// Define array elements
$arr = array(1, 7, 6, 4, 5, 9, 8);
// Get the number of array elements
$size = count($arr);
// Display inversion pair result
$task->printInversionPair($arr, $size);
}
}
InversionPairs::main();
Output
(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment