Created
April 19, 2020 16:23
-
-
Save MagedAhmad/a2fd024b5de5e9c022c60a33e3dea392 to your computer and use it in GitHub Desktop.
Hackerrank minimum Swaps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function minimumSwaps($arr) { | |
| $swaps = 0; | |
| for($i = 0; $i < count($arr); $i++) { | |
| while($arr[$i] != $i + 1) { | |
| $temp = $arr[$i]; | |
| $arr[$i] = $arr[$temp-1]; | |
| $arr[$temp-1] = $temp; | |
| $swaps++; | |
| } | |
| } | |
| return $swaps; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment