sort numbers in php, - php

hi i am using php to learn algorithms, i wanted to convert this psuedocode into php,
for i = 1 to n − 1
minval = A[i]
minindex = i
for j = i to n
if (A[j] < minval)
minval = A[j]
minindex = j
exchange A[i] and A[minindex]
this the corresponding code in php
$A = array(1, 4, 2, 3, 70, 10, 7 );
$n = sizeof($A);
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
if ($A[$i] > $A[$j]){
$temp = $A[$j];
$A[$j] = $A[$i];
$A[$i] = $temp;
}
}
}
print_r($A);
print_r is outputting the array as its original order, why my algorithms doents reorder the array ?

You should check your forloops :
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
should be
for ($i = 0; $i < $n - 1; $i++){
for ($j = $i + 1; $j < $n; $j++){
As the second argument in for is a requirement to continue the loop.

Related

Check If N == M*2 in the array

I'm starting solving problem on leetcode, this problem didn't pass the test cases, this my try:
function checkIfExist($arr) {
$i = 0;
$j = 0;
$n = count($arr);
// loop over the array
for($i; $i < $n; $i++) {
for($j; $j < $n; $j++) {
// check if element of i and j not the same and N*2 = M
if ($i != $j && $arr[$i] * 2 == $arr[$j]) {
return true;
}
}
}
return false;
}
Can please explain to me where I did the error here?
This should work, try this though (it's like one of the sort algorithms). That is weird because only difference is the initialization of the $i and $j
function checkIfExist($arr) {
$n = count($arr);
// loop over the array
for($i = 0; $i < $n - 1; $i++) {
for($j = $i + 1; $j < $n; $j++) {
// check if element of i and j not the same and N*2 = M
if ($i != $j && $arr[$i] * 2 == $arr[$j]) {
return true;
}
}
}
return false;
}
The initializing of $j and $i pointer inside the for-loop done the work
function checkIfExist($arr) {
$n = count($arr);
// loop over the array
for($i = 0; $i < $n; $i++) {
for($j = 0; $j < $n; $j++) {
// check if element of i and j not the same and N*2 = M
if ($i != $j && $arr[$i] * 2 == $arr[$j]) {
return true;
}
}
}
return false;
}

for loop gets executed just once

I have created an array dynamically like this way
$names = array();
for ($i = 0; $i < 100; $i++) {
$names[] = $i;
}
then created part
$parts = count($names) / 20;
and created a sub array then loop through the parts
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i = 0; $i < count($newarray); $i++) {
echo $i;
}
$j = $j + 20;
}
The problem is that this code displays from zero to 19 It doesn't display the other parts
Both the inner and outer loops use the same control variable $i, so just change the inner one...
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i1 = 0; $i1 < count($newarray); $i1++) {
echo $i1;
}
$j = $j + 20;
}

Convert array outputted from DCT to an image in PHP

Using the following code I can get the DCT of an image in PHP. Then I need to convert this back in to the compressed image. How can I achieve that?
<?php
$results = array();
$image1 = "baboon.jpg";
$ima = ImageCreateFromJPEG($image1);
$N1 = imagesx($ima);
$N2 = imagesy($ima);
$rows = array();
$row = array();
for ($j = 0; $j < $N2; $j++) {
for ($i = 0; $i < $N1; $i++)
$row[$i] = imagecolorat($ima, $i, $j);
$rows[$j] = dct1D($row);
}
for ($i = 0; $i < $N1; $i++) {
for ($j = 0; $j < $N2; $j++)
$col[$j] = $rows[$j][$i];
$results[$i] = dct1D($col);
}
print_r($results);
function dct1D($in) {
$results = array();
$N = count($in);
for ($k = 0; $k < $N; $k++) {
$sum = 0;
for ($n = 0; $n < $N; $n++) {
$sum += $in[$n] * cos($k * pi() * ($n + 0.5) / ($N));
}
$sum *= sqrt(2 / $N);
if ($k == 0) {
$sum *= 1 / sqrt(2);
}
$results[$k] = $sum;
}
return $results;
}
?>
Also I need to know how can I add some extra details like another message to this image too.. (image steganography). Please help. Thanks

Undefined offset: 255

Logfiles are full of this error:
Undefined offset: 255 in /var/www/html/site2/functions.inc.php on line 764"
The line 764 is the following:
$counter[$i] = $counter[$x];
The whole function see below.
Can somebody help me to fix it? Thanks.
function Encode($data,$pwd) {
$pwd_length = strlen($pwd);
for ($i = 0; $i < 255; $i++) {
$key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
$counter[$i] = $i;
}
$x = '';
for ($i = 0; $i < 255; $i++) {
$x = ($x + $counter[$i] + $key[$i]) % 256;
$temp_swap = $counter[$i];
$counter[$i] = $counter[$x];
$counter[$x] = $temp_swap;
}
$a = '';
$j = '';
$Zcrypt = '';
for ($i = 0; $i < strlen($data); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $counter[$a]) % 256;
$temp = $counter[$a];
$counter[$a] = $counter[$j];
$counter[$j] = $temp;
$k = $counter[(($counter[$a] + $counter[$j]) % 256)];
$Zcipher = ord(substr($data, $i, 1)) ^ $k;
$Zcrypt .= chr($Zcipher);
}
return $Zcrypt;
}
Your for-loop stops when $i >= 255. You don't want that. I think you want the for-loop to stop at $i>255.
So change
for ($i = 0; $i < 255; $i++) {
into
for ($i = 0; $i <= 255; $i++) {
And you're good to go!

How to heap sort in php?

I writting assignment about heap sort in php. I am in a little bit difficult situation. Please help me. Any one who suggest me the code.
Thanks in advance.
Have you googled this???
Check this out.
Heap Sorting in PHP
<?php
function build_heap(&$array, $i, $t){
$tmp_var = $array[$i];
$j = $i * 2 + 1;
while ($j <= $t) {
if($j < $t)
if($array[$j] < $array[$j + 1]) {
$j = $j + 1;
}
if($tmp_var < $array[$j]) {
$array[$i] = $array[$j];
$i = $j;
$j = 2 * $i + 1;
} else {
$j = $t + 1;
}
}
$array[$i] = $tmp_var;
}
function heap_sort(&$array) {
//This will heapify the array
$init = (int)floor((count($array) - 1) / 2);
// Thanks jimHuang for bug report
for($i=$init; $i >= 0; $i--){
$count = count($array) - 1;
build_heap($array, $i, $count);
}
//swaping of nodes
for ($i = (count($array) - 1); $i >= 1; $i--) {
$tmp_var = $array[0];
$array [0] = $array [$i];
$array [$i] = $tmp_var;
build_heap($array, 0, $i - 1);
}
}
// Demo
$array = array(9,8,7,6,5,4,3,2,1,0,10,1000,0);
heap_sort($array);
print_r($array);
?>

Categories