Skip to content

Instantly share code, notes, and snippets.

@barakpinchovski
Created September 12, 2019 15:08
Show Gist options
  • Save barakpinchovski/81e83a5824d193272fd146461ea960fc to your computer and use it in GitHub Desktop.
Save barakpinchovski/81e83a5824d193272fd146461ea960fc to your computer and use it in GitHub Desktop.
<?php
echo minimumSwaps($arr);
function minimumSwaps($arr) {
$swaps = 0; # Default function value
if (gettype($arr) !== 'array' || !count($arr)) {
return $swaps;
}
$len = count($arr);
while ($currValue = current($arr)) {
$currKey = key($arr) + 1;
if ($currKey !== $currValue) {
$swaps += swap_arr_values($arr, key($arr), $currValue - 1);
}
else {
next($arr);
}
}
return $swaps;
}
function swap_arr_values(&$arr, $i, $j) {
$temp = $arr[$j];
$arr[$j] = $arr[$i];
$arr[$i] = $temp;
return 1;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment