Categories
Code Area PHP

Sorting Array by using Linear Algorithm

Sorting Array by using Linear Algorithm are given below.

CODE:

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: RANA
 * Date: 6/13/13
 * Time: 4:23 PM
 * To change this template use File | Settings | File Templates.
 */
class LinearAlgorithm
{
    private  $MyArray;

    /**
     * @param $SortingArray
     */
    Public function __construct($SortingArray){
        $this->MyArray = $SortingArray;
    }

    /**
     * @return mixed as a Sorting the array Element.
     */
    public function SortingNumber(){
        foreach($this->MyArray as $Element => $value){
            $PostElement = $Element + 1;
            while (($PostElement < count($this->MyArray) && ($this->MyArray[$PostElement] < $this->MyArray[$Element])))
            {
                $Temp = $this->MyArray[$Element];
                $this->MyArray[$Element] = $this->MyArray[$PostElement];
                $this->MyArray[$PostElement] = $Temp;

                if ($Element > 0)
                {
                    $Element--;
                }
                $PostElement--;
            }
        }
        return $this->MyArray;
    }

    /**
     * @param Output the output.
     */
    public function GetResult(){
        foreach($this->SortingNumber() as $Result){
            echo $Result."<br/>";
        }
    }
}

$Array = array(5,10,266,255,1656,36,66,1,5665);
$Object = new LinearAlgorithm( $Array);
$Object->GetResult();

 

OUTPUT:

1
5
10
36
66
255
266
1656
5665

 

 

By Sohel Rana

PHP Programmer, Software Engineer & Technology lover

Leave a Reply

Your email address will not be published. Required fields are marked *