-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion-sort.php
41 lines (38 loc) · 1017 Bytes
/
insertion-sort.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insertion Sort Progam in PHP with a while loop</title>
</head>
<body>
<?php
function insertionSort($data)
{
$n=count($data);
$next=null;
for($i=1; $i<$n; $i++)//outer loop
{
$next=$data[$i];
for($j=$i-1; $j>=0; $j--)//inner loop
{
if( $data[$j]>$next )//change > to < for descending order
{
$data[$j+1]=$data[$j];
}
else
{
break;
}
}
$data[$j+1]=$next; // insert the next value to the correct postion of the already sorted elements
}
return $data;
}
echo implode(",",array(43,23,4,11,2,88,76,46));
echo "<br>";
echo implode(",",insertionSort(array(43,23,4,11,2,88,76,46)));
?>
</body>
</html>