PHP
  Home arrow PHP arrow Page 2 - Collections and Sorting Continued
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Collections and Sorting Continued
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-04-04


    Table of Contents:
  • Collections and Sorting Continued
  • Sort Algorithms of Quadratic Complexity
  • Sort Algorithms of n log n Complexity
  • Metrics

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Collections and Sorting Continued - Sort Algorithms of Quadratic Complexity
    ( Page 2 of 4 )

    Bubble sort, insertion sort, selection sort and shell sort are algorithms of quadratic complexity. In the first part of this article, we implemented the bubble sort with the following code in the Sort() method from our People Collection class:

            public function Sort()
      {
            for ($i = $this->data->count() - 1; $i >= 0; $i--)
            {
                  $flipped = false;
                  for ($j = 0; $j < $i; $j++)
                  {
                        if (strcmp($this->data[$j]->GetSortKey(), 
                              $this->data[$j + 1]->GetSortKey()) > 0)
                        { 
                              $tmp = $this->data[$j];
                              $this->data->offsetSet($j, $this->data
    [$j + 1]);
                              $this->data->offsetSet($j + 1, $tmp);
                              $flipped = true;
                        }
                  }
                  if (!$flipped)
                        return;
            }
      }

    Very simple and straightforward. I will not explain the code in the algorithms in this article, I leave it to you the reader as an exercise. You will benefit from learning how these algorithms work if you do not know already! Let’s have a look at the code for an insertion sort:

            public function InsertionSort()
      {
           for ($i = 1; $i < $this->data->count(); $i++)
            {
                  $j = $i;
                  $tmp = $this->data[$i];
                  while (($j > 0) && (
                        strcmp($this->data[$j - 1]->GetSortKey(), 
                              $tmp->GetSortKey()) > 0)
                        )
                  {
                        $this->data->offsetSet($j, $this->data[$j -
    1]);
                        $j--;
                  }
                  $this->data->offsetSet($j, $tmp);
            }
      }

    Now a selection sort:

            public function SelectionSort()
      {
            for ($i = 0; $i < $this->data->count(); $i++)
            {
                  $min = $i;
                  $j = 0;
                   
                  for ($j = $i + 1; $j < $this->data->count(); $j++)
                  {
                        if (strcmp($this->data[$j]->GetSortKey(), 
                              $this->data[$min]->GetSortKey()) < 0)
                        {
                              $min = $j;
                        }
                  }                 

                  $tmp = $this->data[$min];
                  $this->data->offsetSet($min, $this->data[$i]);
                  $this->data->offsetSet($i, $tmp);
            }
      }

    And for the last quadratic complexity sort, the shell sort:

            public function ShellSort()
      {
            $increment = 3;                 

            while ($increment > 0)
            {
                  for ($i = 0; $i < $this->data->count(); $i++)
                  {
                        $tmp = $this->data[$i];
                        $j = $i;                       

                        while ($j >= $increment)
                        {
                              if ($this->data[$j - $increment])
                              {
                                    if (strcmp($this->data[$j -
    $increment]->GetSortKey(), 
                                          $tmp->GetSortKey()) > 0)
                                    {
                                          $this->data->offsetSet($j, 
                                                $this->data[$j -
    $increment]);
                                          $j -= $increment;
                                    }
                              }
                        }
                        $this->data->offsetSet($j, $tmp);
                  }
                   
                  if ($increment % 2 != 0)
                        $increment = ($increment - 1) / 2;
                  elseif ($increment == 1)
                        $increment = 0;
                  else 
                        $increment = 1;
            }
     }

    All of these sorts are slow. The shell sort is dramatically faster than the rest, but still slow relative to the algorithms. Before we delve into performance, though, I want to show the code for the n log n complexity algorithms.



     
     
    >>> More PHP Articles          >>> More By David Fells
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek