-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion Sort - Part 2.php
60 lines (45 loc) · 1004 Bytes
/
Insertion Sort - Part 2.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* @author: syed ashraf ullah
* Date: 21/04/2020
*/
// Complete the insertionSort1 function below.
function insertionSort1($n, $arr)
{
$i = 0;
while (++$i < $n) {
$k = $i;
$val = $arr[$k];
// Sort from current index
while (--$k >= 0 && $val < $arr[$k]) {
$arr[$k+1] = $arr[$k];
}
$arr[$k+1] = $val;
// Print current array
foreach ($arr as $value) {
echo $value.' ';
}
// new line CMD
// echo PHP_EOL;
// Browser
echo '</br>';
}
}
/**
* Sample input #1
*/
$n = 6;
$arr = array(1, 4, 3, 5, 6, 2);
/**
* Sample input #2
*/
// $n = 7;
// $arr = array(3, 4, 7, 5, 6, 2, 1);
insertionSort1($n, $arr);
return;
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $n);
fscanf($stdin, "%[^\n]", $arr_temp);
$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
insertionSort1($n, $arr);
fclose($stdin);