PHP : koodikatkendid

From mmmv_kos
Jump to: navigation, search


Sorteerimine

array_multisort ON TEGELIKULT PURUKS:

$ar1=array(10, 100, 100, 0);
$ar2=array(1, 3, 2, 4);

$i_len=count($ar1);
$x_elem=NULL;
for ($i=0; $i < $i_len; $i++) {
    echo('('.$ar1[$i].' -- '.$ar2[$i].')  ');
} // for

array_multisort($ar1, $ar2);

echo('<br/><br/>Pärast sorteerimist:<br/><br/>');

$i_len=count($ar1);
$x_elem=NULL;
for ($i=0; $i < $i_len; $i++) {
    echo('('.$ar1[$i].' -- '.$ar2[$i].')  ');
} // for

// http://php.net/manual/en/function.array-multisort.php

/*
(10 -- 1) (100 -- 3) (100 -- 2) (0 -- 4)

Pärast sorteerimist:

(0 -- 4) (10 -- 1) (100 -- 2) (100 -- 3)
 */


usort ON TEGELIKULT PURUKS, aga enda-kirjutatu toimib, nagu harilikult :-D

error_reporting(-1);
ini_set('display_errors', 'On');


$arht_records=array();
array_push($arht_records, array(44, 9));
array_push($arht_records, array(7, 23));
array_push($arht_records, array(5, 23));
array_push($arht_records, array(5, 10));
array_push($arht_records, array(4, 10));
array_push($arht_records, array(0, 766));

function s_arht2html_t1(&$arht_in) {
    $i_len=count($arht_in);
    $arht_record=NULL;
    $s_out='';
    for ($i=0; $i < $i_len; $i++) {
        $arht_record=$arht_in[$i];
        $s_out=$s_out.'('.$arht_record[0].' -- '.$arht_record[1].') '.
            "<br/>\n";
    } // for
    return $s_out;
} // s_arht2html_t1

echo(s_arht2html_t1($arht_records));

function i_comparizon_1(&$arht_record_0, &$arht_record_1) {
    try {
        // http://php.net/manual/en/function.usort.php
        $x_0=$arht_record_0[1];
        $x_1=$arht_record_1[1];
        //echo(' '.$x_0.' '.$x_1."<br/>\n");
        if ($x_0 == $x_1) {
            return 0;
        } // if
        if ($x_0 < $x_1) {
            return (-1);
        } else {
            return 1;
        } // else
    } catch (Exception $err_exception) {
        throw(new Exception($err_exception."\n".
            "GUID='64852dfe-667d-45cc-b943-602370909fd7'"));
    } // catch
} // i_comparizon_1

// It's a copy of the standard usort, except
// that unlike the usort, this sorting implementation
// actually sorts correctly, is not flawed.
// It mimics the usort interface.
// http://php.net/manual/en/function.usort.php
function sirel_quicksort_t1(&$arht_in, $func_comparison) {
    try {
        $i_arht_in_len=count($arht_in);
        if ($i_arht_in_len < 2) { // 0, 1
            return;
        } // if
        $x_watershed=$arht_in[0];
        $arht_smaller=array();
        $arht_gt_or_eq_to=array(); // will contain the watershed
        $x_elem=NULL;
        $i_comparison_result=null;
        $b_all_elements_are_equal=true;
        for ($i=1; $i < $i_arht_in_len; $i++) {
            $x_elem=$arht_in[$i];
            $i_comparison_result=$func_comparison($x_elem, $x_watershed);
            if ($i_comparison_result != 0) {
                // May be the       "!==" might not be practical here.
                $b_all_elements_are_equal=false;
            } // if
            if ($i_comparison_result < 0) {
                array_push($arht_smaller, $x_elem);
            } else {
                array_push($arht_gt_or_eq_to, $x_elem);
            } // else
        } // for
        if ($b_all_elements_are_equal === true) {
            // Yet another way to break infinite recursion.
            return;
        } // if
        //--------
        // As the new watershed will be chosen from
        // $arht_gt_or_eq_to[0], a way to avoid an
        // infinite recursion is to place the old watershed
        // to the last position.
        array_push($arht_gt_or_eq_to, $x_watershed);
        //--------
        $i_arht_smaller_len=count($arht_smaller);
        $i_arht_gt_or_eq_to_len=count($arht_gt_or_eq_to);

        sirel_quicksort_t1($arht_smaller, $func_comparison);
        sirel_quicksort_t1($arht_gt_or_eq_to, $func_comparison);
        $ix_arht_in=0;
        for ($i=0; $i < $i_arht_smaller_len; $i++) {
            $x_elem=$arht_smaller[$i];
            $arht_in[$ix_arht_in]=$x_elem;
            $ix_arht_in++;
        } // for
        for ($i=0; $i < $i_arht_gt_or_eq_to_len; $i++) {
            $x_elem=$arht_gt_or_eq_to[$i];
            $arht_in[$ix_arht_in]=$x_elem;
            $ix_arht_in++;
        } // for
    } catch (Exception $err_exception) {
        throw(new Exception($err_exception."\n".
            "GUID='29631943-7625-40c4-a813-602370909fd7'"));
    } // catch
} // sirel_quicksort_t1

// The flawed standard implementation
//usort($arht_records, 'i_comparizon_1');

sirel_quicksort_t1($arht_records, 'i_comparizon_1');

echo('<br/><br/>'.s_arht2html_t1($arht_records));

/* The output:
(44 -- 9)
(7 -- 23)
(5 -- 23)
(5 -- 10)
(4 -- 10)
(0 -- 766)


(44 -- 9)
(4 -- 10)
(5 -- 10)
(7 -- 23)
(5 -- 23)
(0 -- 766)
 */