forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created Pancake Sort in php (jainaman224#2976)
* Created using Colaboratory * Deleted accidentally added file * Adding Pancake_Sort.php file * Updated Pancake_Sort.php file * Updated Pancake_Sort.php and made the changes
- Loading branch information
1 parent
d5f9a31
commit 432c23b
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
//function which actually sorts the array | ||
function pancakeSort(&$arr, $num) | ||
{ | ||
//start with whole array as input and reduces size by 1 on every iteration | ||
for ($currentSize = $num; $currentSize > 1; $currentSize--) | ||
{ | ||
//finds the index of the maximum element in the array | ||
$maxIndex = findMaxIndex($arr, $currentSize); | ||
//if the maximum index is not at the end of the current array then move the element to the end | ||
if ($maxIndex != $currentSize-1) | ||
{ | ||
//moves maximum number to the beginning | ||
reverseArray($arr, $maxIndex); | ||
//moves maximum number to the end by reversing the array | ||
reverseArray($arr, $currentSize-1); | ||
} | ||
} | ||
} | ||
|
||
//reverses the array from index 0 to index | ||
function reverseArray(&$arr, $index) | ||
{ | ||
$startIndex = 0; | ||
//reverses till the startindex is not greater than index provided | ||
while ($startIndex < $index) | ||
{ | ||
//swapping of elements | ||
$temp = $arr[$startIndex]; | ||
$arr[$startIndex] = $arr[$index]; | ||
$arr[$index] = $temp; | ||
//increasing starting Index of an array | ||
$startIndex++; | ||
//decreasing ending index of an array | ||
$index--; | ||
} | ||
} | ||
|
||
//Find the index of the maximum element in an array | ||
function findMaxIndex($arr, $num) | ||
{ | ||
$maxIndex = 0; | ||
for ($i = 0; $i < $num; $i++) | ||
{ | ||
if ($arr[$i] > $arr[$maxIndex]) | ||
{ | ||
$maxIndex = $i; | ||
} | ||
} | ||
return $maxIndex; | ||
} | ||
|
||
$arr = array(112,65,12,56,100,93,20); | ||
//Counts the number of elements in the array | ||
$num = count($arr); | ||
|
||
pancakeSort($arr, $num); | ||
|
||
echo("Sorted Array is:\n"); | ||
//Prints the sorted array | ||
for($i = 0;$i < $num; $i++) | ||
{ | ||
print($arr[$i]." "); | ||
} | ||
|
||
return 0; | ||
|
||
/* | ||
Input: | ||
112,65,12,56,100,93,20 | ||
Output: | ||
12,20,56,65,93,100,112 | ||
*/ | ||
?> |