Programación Web y Scripting > PHP

 Algoritmos De Ordenamiento Y Busqueda En Php

(1/2) > >>

<kito>:
HOLAAA!!!
navegando por las guebs me encontre con esta y además chilensis,  me alegro por la discución de los temas, MI PREGUNTA ES LA SGTE.


si algun programador con experiencia en PHP me puede alludar a encontrar la solución e implementaión de los algoritmos de ordenamiento:
-seleccion
-inserciòn
-intercambio(burbuja)
-mezcla
 
y busqueda:

-secuencial
-binaria
-arbol de busqueda
-busqueda hash

tengo conocimientos en PHP, pero no experto, soy un egresado de ING. Informática y me alegraria si alguien me ay :alien: uda con esto o con ideas u opiniones.

GRACIAS!!!!!

<kito>:
ehhhh.. los datos se extraen de un .txt    formateado

    con los campos, por ej:
rut     ----------> id
nombre
ciudad
año ingreso
carrera

espero una discución, nos vemos Saludos a To2 los Inforcuaticos.
 desde la VII REGIÓN!!!! :alien:

<kito>:
ahi hay algo por favor respetar al autor de este codigo ok!


//**************************************
    //    
    // Name: Binary search
    // Description:Binary search algorithm f
    //     unction. Takes an array as an argument a
    //     nd the element
    to be searched for in the array. Returns '1' if found, '0' if not.
    The code can be modified slightly to return the array index of the found element.
    // By: Bhushan.
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=755&lngWId=8    //for details.    //**************************************
    //    
   
    <?php
    function BinarySearch($ArrayToSearch/*array to search through*/, $SearchFor/*element to search for*/)
    {
    sort($ArrayToSearch);//must sort the array
   
    //index   
    $first=0;
    $last=count($ArrayToSearch)-1;
    $mid=($first+$last)/2;
    $SearchFor=strval(trim($SearchFor));
    while
    (
    ($first<=$last)&&
        (strval(trim($ArrayToSearch[$mid]))!=$SearchFor)
    )   
    {
    if(strcmp(strtolower($SearchFor),strtolower($ArrayToSearch[$mid]))<0)
        {$last=$mid-1;}//search the upper half
        else
        if(strcmp(strtolower($SearchFor),strtolower($ArrayToSearch[$mid]))>0)
       {$first=$mid+1;}//search the lower half
       
        $mid=($first+$last)/2;//new mid point
    }   
   
    if(strval(trim($ArrayToSearch[$mid]))==$SearchFor)    
    //{return $mid;} --> if your objectiv
    //     e is to return the index
       {return 1;}
    else
    {return 0;}
    //{return -1;} --> if your objective
    //     is to return the index
    }
    ?>


///ordenamiento burbuja
//**************************************
    //    
    // Name: bubble sort
    // Description:simple bubble sort
    // By: Bhushan.
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=756&lngWId=8    //for details.    //**************************************
    //    
   
    <?php
    function bubblesort($a1,$a2)
    {
    for($i = sizeof($a1); $i >= 1; $i--)
    {
    for($j = 1; $j <= $i; $j++)
    {
    if($a1[$j-1] > $a1[$j])
    {
    $t = $a1[$j-1];
    $t2 = $a2[$j-1];
    $a1[$j-1] = $a1[$j];
    $a2[$j-1] = $a2[$j];
    $a1[$j] = $t;
    $a2[$j] = $t2;
    }
    }
    }
    }
    ?>

//otro ordenamiento burbuja

//**************************************
    //    
    // Name: Bubble Sort Algorithm
    // Description:Simple bubble sort algori
    //     thm.
    // By: Alberto Sartori
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=761&lngWId=8    //for details.    //**************************************
    //    
   
    <?php
    //BUBBLE SORT ALGORITHM
    function bubblesort(&$incoming_vector,$outgoing_vector) {
    $chain=0;
    $a=0;
    for($a=$outgoing_vector; $a>0; $a--) {
    $chain=1;
    for($v=0; $v<$outgoing_vector ;$v++) {
    if ($incoming_vector[$v] > $incoming_vector[$v+1]) {
        $aux=$incoming_vector[$v];
    $incoming_vector[$v]=$incoming_vector[$v+1];
    $incoming_vector[$v+1]=$aux;
    $chain=$v;
    }
    }
    $outgoing_vector=$chain;
    }
    }
    ?>      
//ordenamiento quk sort
//**************************************
    //    
    // Name: Quck Sort function
    // Description:Quck Sort function.
    // By: Bhushan.
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=757&lngWId=8    //for details.    //**************************************
    //    
   
    // Quck Sort function
    function qsort( &$rowdata, $sortBy, $first, $last )
    {
    $lo = $first;
    $up = $last;
    $i = $first + $last;
    $bound = strval($rowdata[($i - $i%2)/2][$sortBy]);
    while ( $lo <= $up)
    {
    while( ($lo <= $last - 1) && (strval($rowdata[$lo][$sortBy]) < $bound))
    {
    $lo++;
    }
    while (($up >= 1) && ($bound < strval($rowdata[$up][$sortBy])) )
    {
    $up--;
    }
    if ($lo < $up)
    {
    $tmp = $rowdata[$up];
    $rowdata[$up] = $rowdata[$lo];
    $rowdata[$lo] = $tmp;
    $up--;
    $lo++;
    }
    else
    {
    $lo++;
    }
    }
    if ($first < $up) qsort($rowdata, $sortBy, $first, $up);
    if ($up + 1 < $last) qsort($rowdata, $sortBy, $up + 1, $last);
    }

//////////////////////////////////////////////////////////////////////////
usar funcion FILE para capturar el txt a un array,
faltan algunas cosas relacionadas con la programacion, pero va luegooo...
nos vemos.

Informix:
Kito disculpame si te molesta mi comentario pero si eres un egresado en Ing en Informatica al menos deberias de saber como se implementan los algoritmos, mi recomendación es que primero busques informacion sobre los algoritmos, los leas y despues leas un poco sobre php al ser ingeniero seguro trabajaste sobre C y C++ entonces php no sera un gran problema ya que es muy parecido a estos lenguajes, y para php te recomiendo la siguiente pagina web

http://www.php.net

 :good:

Blag:
Estoy totalmente de acuerdo con Informix, esos son algoritmos basicos que se aprenden en cualquier carrera de sistemas o informatica. Ya sea en C++ o Java.


--- Citar ---ahi hay algo por favor respetar al autor de este codigo ok!

--- Fin de la cita ---

 :angry:  La duda era tuya no nuestra, asi que no menciones algo que es obvio y que ademas es una regla entre los programadores.

Saludos,

Blag  :devil:

Navegación

[0] Índice de Mensajes

[#] Página Siguiente

Ir a la versión completa