array sorting custom algorithm using (PHP) - php

http://sandbox.onlinephpfunctions.com/code/971f4a6c9dfe80ec6277dd89653ea160af3c68e4
I can't find solution .. Please, help to return changed array
Code
<?php
function bell_sort($arr, $head, $tail, $queue) {
if($head != $tail){
$min = $arr[$head];
$min_index = $head;
for($i = $head; $i <= $tail; ++$i){
if($arr[$i] < $min){
$min = $arr[$i];
$min_index = $i;
}
}
if($queue){
$tmp = $arr[$head];
$arr[$head] = $arr[$min_index];
$arr[$min_index] = $tmp;
$head++;
}else{
$tmp = $arr[$tail];
$arr[$tail] = $arr[$min_index];
$arr[$min_index] = $tmp;
$tail--;
}
bell_sort($arr, $head, $tail, !$queue);
}else{
return $arr;
}
}
$n = 10;
for($i = 0; $i < $n; ++$i){
$arr[$i] = rand(0, 100);
}
$head = 0;
$tail = count($arr) - 1;
$queue = 1;
$new_arr = bell_sort($arr, $head, $tail, $queue);
var_dump($new_arr);

You're missing a return statement in one of the recursive paths in the bell_sort function.
if($queue){
$tmp = $arr[$head];
$arr[$head] = $arr[$min_index];
$arr[$min_index] = $tmp;
$head++;
}else{
$tmp = $arr[$tail];
$arr[$tail] = $arr[$min_index];
$arr[$min_index] = $tmp;
$tail--;
}
bell_sort($arr, $head, $tail, !$queue);
Change the line above line to
return bell_sort($arr, $head, $tail, !$queue);
and it will work

Related

PHP Create an encode algorithm?

function deCode_Me($strContent, $cKey){
$hRet = 0;
$sRet = "";
foreach( str_split($strContent) as $nKey ){
$sRet .= chr(ord($nKey) ^ ord($cKey[$hRet++ % strlen($cKey)]));
}
return $sRet;
}
How can I make the encoder for the above resolver.
the exact opposite?
function encode_Me($strContent, $cKey)
{
$key = $cKey;
$text = $strContent;
$outText = '';
for ($i = 0; $i < strlen($text); $i++)
{
for ($j = 0; $j < strlen($key); $j++, $i++)
{
$outText.= $text
{
$i} ^ $key
{
$j};
}
}
return $outText;
}
worked ! :)

PHP - How to create all possibility from a string

Here, there is a example string "XjYAKpR" .. how to create all new string possibility with that string ??
I've tried before
function containAllRots($s, $arr) {
$n = strlen($s);
$a = array();
for ($i = 0; $i < $n ; $i++) {
$rotated = rotate(str_split($s), $i);
$a[] = $rotated;
}
print_r($a);die();
if (array_diff($arr, $a)) {
return True;
}
else
{
return False;
}
}
I make 2 function rotate and generate
function rotate($l, $n) {
$b = $l[$n];
$sisa = array_values(array_diff($l, array($b)));
for ($i = 0; $i < count($sisa) ; $i++) {
$random[] = generate($sisa, $b);
}
print_r($random);die();
$hasil = $l[$n] . implode("",$random);
return $hasil;
}
function generate($sisa, $b) {
$string = implode("",$sisa);
$length = count($sisa);
$size = strlen($string);
$str = '';
for( $i = 0; $i < $length; $i++ ) {
$str .= $string[ rand( 0, $size - 1 ) ];
}
Here there is a pair of functions that lets you calculate a permutation set
(no repetitions are taken in account)
function extends_permutation($char, $perm) {
$result = [];
$times = count($perm);
for ($i=0; $i<$times; $i++) {
$temp = $perm;
array_splice($temp, $i, 0, $char);
array_push($result, $temp);
}
array_push($result, array_merge($perm, [$char]));
return $result;
}
function extends_set_of_permutations($char, $set) {
$step = [];
foreach ($set as $perm) {
$step = array_merge($step, extends_permutation($char, $perm));
}
return $step;
}
you can use them to generate the required set of permutations. Something like this:
$seed = "XjYAKpR";
// the first set of permutations contains only the
// possible permutation of a one char string (1)
$result_set = [[$seed[0]]];
$rest = str_split(substr($seed,1));
foreach($rest as $char) {
$result_set = extends_set_of_permutations($char, $result_set);
}
$result_set = array_map('implode', $result_set);
sort($result_set);
At the end of the execution you will have the 5040 permutations generated by your string in the result_set array (sorted in alphabetical order).
Add a char and you will have more than 40000 results.
The functions are quite naive in implementation and naming, both aspects can be improved.

Making array after explode

I want to create an array from an input string. Before this code, I've tried explode, but the array remains length 1. Each string that I've tried is still one in array[0]. Here's my code so far:
public function word()
{
$kata = array($this->kal->getHasil());
if (!empty($kata)) {
$n = count($kata)
for ($i = 0; $i < $n; $i++) {
$imin = $i;
for ($j = $i; $j < $n; $j++) {
if ($kata[$j] < $kata[$imin]) {
$imin = $j;
}
}
$temp = $kata[$i];
$kata[$i] = $kata[$imin];
$kata[$imin] = $temp;
}
for ($i = 0; $i < $n; $i++) {
echo "$kata[$i] ";
}
}
}
public function tokenize()
{
$temp = $this->kal->getHasil();
$token = explode(" ", $temp);
return $token;
}
$hasil = $pp->tokenize();
for ($i = 0; $i < sizeof($hasil); $i++) {
$st = new stemming();
$hasil[$i] = $pp->singkatan($hasil[$i]);
$hasil[$i] = $st->stem($hasil[$i]);
$hasil[$i] = $pp->stopWord($hasil[$i]);
//echo "$hasil[$i] ";
$hb = new hitungBobot($hasil[$i]);
$hb->word();
}
How would I fix this?
You can use a globar var, see the code:
public function word(){ $kata = array($this->kal->getHasil());
global $output;
if(!empty($kata)){
$ar= count($kata)
$output += $ar;
}
public function tokenize() {
$temp = $this->kal->getHasil();
$token = explode(" ",$temp);
return $token;
}
$output = 0;
$hasil = $pp->tokenize();
for($i=0; $i<sizeof($hasil); $i++) {
$st = new stemming();
$hasil[$i] = $pp->singkatan($hasil[$i]);
$hasil[$i] = $st->stem($hasil[$i]);
$hasil[$i] = $pp->stopWord($hasil[$i]);
//echo "$hasil[$i] ";
$hb = new hitungBobot($hasil[$i]);
$hb->word();
}
echo $output;

php - permutation - possible number

already look around but cant find what i want for PHP.
just say i have a number : 1234 ( can be splitted first into array )
and i want to get how many number combination possible for 2 digits, 3 digits , and 4 digits
for example :
possible 4 digits will be :
1234,1243,1324,1342, and so on. ( i dont know how many more )
possible 2 digits will be :
12,13,14,21,23,24,31,32,34,41,42,43
the closest one i get is :
$p = permutate(array('1','2','3','4'));
$result = array();
foreach($p as $perm) {
$result[]=join("",$perm);
}
$result = array_unique($result);
print join("|", $result);
function permutate($elements, $perm = array(), &$permArray = array()){
if(empty($elements)){
array_push($permArray,$perm); return;
}
for($i=0;$i<=count($elements)-1;$i++){
array_push($perm,$elements[$i]);
$tmp = $elements; array_splice($tmp,$i,1);
permutate($tmp,$perm,$permArray);
array_pop($perm);
}
return $permArray;
}
but how can i edit this so i can display for 3 and 2 digits ?
Thanks
i got what i want
it's from #mudasobwa link. and i edit to what i want.
<?php
$in = array(1,2,3,4,5,6);
$te = power_perms($in);
// print_r($te);
$thou=0;
$hun =0;
$pu = 0;
for($i=0;$i<count($te);$i++)
{
$jm = count($te[$i]);
for($j=0;$j<$jm;$j++)
{
$hsl[$i] = $hsl[$i] . $te[$i][$j];
}
if($hsl[$i] >=100 && $hsl[$i] < 1000 )
{
$ratus[$hun] = intval($hsl[$i]);
$hun = $hun + 1;
}
if($hsl[$i] <100 && $hsl[$i] >=10)
{
$pul[$pu] = intval($hsl[$i]);
$pu = $pu + 1;
}
if($hsl[$i] >=1000 && $hsl[$i] < 10000)
{
$th[$thou] = intval($hsl[$i]);
$thou = $thou + 1;
}
}
$th=array_unique($th);
$pul = array_unique($pul);
$ratus = array_unique($ratus);
sort($ratus);
sort($pul);
sort($th);
print_r($th);
function power_perms($arr) {
$power_set = power_set($arr);
$result = array();
foreach($power_set as $set) {
$perms = perms($set);
$result = array_merge($result,$perms);
}
return $result;
}
function power_set($in,$minLength = 1) {
$count = count($in);
$members = pow(2,$count);
$return = array();
for ($i = 0; $i < $members; $i++) {
$b = sprintf("%0".$count."b",$i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1') $out[] = $in[$j];
}
if (count($out) >= $minLength) {
$return[] = $out;
}
}
// usort($return,"cmp"); //can sort here by length
return $return;
}
function factorial($int){
if($int < 2) {
return 1;
}
for($f = 2; $int-1 > 1; $f *= $int--);
return $f;
}
function perm($arr, $nth = null) {
if ($nth === null) {
return perms($arr);
}
$result = array();
$length = count($arr);
while ($length--) {
$f = factorial($length);
$p = floor($nth / $f);
$result[] = $arr[$p];
array_delete_by_key($arr, $p);
$nth -= $p * $f;
}
$result = array_merge($result,$arr);
return $result;
}
function perms($arr) {
$p = array();
for ($i=0; $i < factorial(count($arr)); $i++) {
$p[] = perm($arr, $i);
}
return $p;
}
function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) {
unset($array[$delete_key]);
if(!$use_old_keys) {
$array = array_values($array);
}
return TRUE;
}
?>

what i use in place of indexOf in php

here's my code so please tell me what i used in place of indexOf usind php because there is no indexOf function in php .
function generate($arrLength)
{
$arr = array();
$n = 0;
$start = 10;
$end = 20;
for($i=0; $i < $arrLength; $i++)
{
do{
$n = $start + round(rand()*($end - $start));
}while($arr.indexOf($n) !== -1);
$arr[$i] = $n;
}
return $arr;
}
$generatedArr = generate(4);
You can use array_search() in this case:
function generate($arrLength) {
$arr = array();
$n = 0;
$start = 10;
$end = 20;
for($i=0; $i < $arrLength; $i++) {
do {
$n = $start + round(rand()*($end - $start));
} while(array_search($n, $arr) !== false);
$arr[$i] = $n;
}
return $arr;
}
$generatedArr = generate(4);
echo '<pre>';
print_r($generatedArr);

Categories