-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice.php
More file actions
51 lines (43 loc) · 1.02 KB
/
Practice.php
File metadata and controls
51 lines (43 loc) · 1.02 KB
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
<?php
/* //Task 1: Print Unique Numbers from an Array
$arr = [3, 2, 5, 2, 2, 2];
for($i=0; $i<6; $i++){
$isDuplicate = false;
for($j=0; $j<$i; $j++){
if($arr[$i] == $arr[$j]){
$isDuplicate = true;
}
}
if(!$isDuplicate){
echo $arr[$i]. " ";
}
}
*/ // Ans: 3 2 5
/* //Task 2: Print All Numbers Using Callback Function
function PrintNumbers($n, $cnt){
if($cnt <= $n){
echo $cnt. " ";
$cnt++;
PrintNumbers($n, $cnt);
}
}
PrintNumbers(5, 1);
*/ // Ans: 1 2 3 4 5
$arr = [64, 34, 87, 12, 81, 43, 23];
$n = count($arr);
for($i=0; $i<$n-1; $i++){
for($j=0; $j<$n-$i-1; $j++){
if($arr[$j] > $arr[$j+1]){
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];
$arr[$j+1] = $tmp;
}
}
}
print_r($arr);
//stp 1: 34, 64 => 0, total => 5
//stp 2: 34, 64, 87 => 1 No swap
//stp 3: 34, 64, 12, 87 => 2
//stp 4: 34, 64, 12, 81, 87 => 3
//stp 5: 34, 64, 12, 81, 43, 87 => 4
//stp 6: 34, 64, 12, 81, 43, 23, 87 => 5