8000 Added php by following proper directory structure · kishore-code/Algorithms-1@1639d07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1639d07

Browse files
authored
Added php by following proper directory structure
1 parent bcbe91b commit 1639d07

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
// PHP program to
3+
// sort array using
4+
// pancake sort
5+
6+
/* Reverses arr[0..i] */
7+
function flip(&$arr, $i)
8+
{
9+
$start = 0;
10+
while ($start < $i)
11+
{
12+
$temp = $arr[$start];
13+
$arr[$start] = $arr[$i];
14+
$arr[$i] = $temp;
15+
$start++;
16+
$i--;
17+
}
18+
}
19+
20+
// Returns index of the
21+
// maximum element in
22+
// arr[0..n-1]
23+
function findMax($arr, $n)
24+
{
25+
$mi = 0;
26+
for ($i = 0; $i < $n; ++$i)
27+
if ($arr[$i] > $arr[$mi])
28+
$mi = $i;
29+
return $mi;
30+
}
31+
32+
// The main function that
33+
// sorts given array using
34+
// flip operations
35+
function pancakeSort(&$arr, $n)
36+
{
37+
// Start from the complete
38+
// array and one by one
39+
// reduce current size
40+
// by one
41+
for ($curr_size = $n; $curr_size > 1; --$curr_size)
42+
{
43+
// Find index of the
44+
// maximum element in
45+
// arr[0..curr_size-1]
46+
$mi = findMax($arr, $curr_size);
47+
48+
// Move the maximum
49+
// element to end of
50+
// current array if
51+
// it's not already
52+
// at the end
53+
if ($mi != $curr_size-1)
54+
{
55+
// To move at the end,
56+
// first move maximum
57+
// number to beginning
58+
flip($arr, $mi);
59+
60+
// Now move the maximum
61+
// number to end by
62+
// reversing current array
63+
flip($arr, $curr_size-1);
64+
}
65+
}
66+
}
67+
68+
// A utility function to print
69+
// n array of size n
70+
function printArray($arr, $n)
71+
{
72+
for ($i = 0; $i < $n; ++$i)
73+
print($arr[$i]." ");
74+
}
75+
76+
// Driver code
77+
$arr = array(23, 10, 20, 11, 12, 6, 7);
78+
$n = count($arr);
79+
80+
pancakeSort($arr, $n);
81+
82+
echo("Sorted Array \n");
83+
printArray($arr, $n);
84+
85+
return 0;
86+
87+
// This code is contributed by vidz-1
88+
?>
89+
<!-- -->

0 commit comments

Comments
 (0)
0