Sorting By Exchanging
Posted on Mon 4th October 2010 8.24PM In algorithms

We come now to the family of sorting algorithms: exchange or transposition methods which systematically interchange pairs of elements that are out of order until no more such pairs exist.

The bubble sort. We compare K(1) with K(2), interchange R(1) with R(2) if the keys are out of order; then we do the same to R(2) and R(3), R(3) and R(4) etc. During this sequence of operations, records with large keys will move up. Repetitions of the process wil get the apropriate records into positions R(N), R(N-1), R(N-2) etc. so that all records will ultimately be sorted.

The method is called bubble sorting because large elements bubble up to their proper position. After each pass through the file, it is not hard to see that all records above and including the last one to be exchanged must be in their final position, so they need not be examined on subsequence passes.

Algorithm B (Bubble Sort). Records R(1),...,R(N) are rearranged
in place; after sorting is complete their keys will be in
order, K(1)<=K(2)<= ...<=K(N).

1. Set BOUND <-- N (BOUND is the highest index for which
   the record is known to be in its final position).

2. Set t <-- 0, Perform step 3 for j=1,2,...,BOUND - 1, and then go to step 4

3. If K(j) > K(j+1) , interchange R(j) <--> R(j+1) and set t <-- j.

4. If t = 0 , the algorithm terminates, otherwise set BOUND <-- t 
and return to step 2.

      No comments.

PHP Accordion Class using MooTools
Posted on Fri 13th November 2009 8.02PM In PHP

I've created a PHP Accordion class to help make the development of accordions easier and fast.
class Accordion {

      private $toggler_class;

      private $item_class;

      private $items;

      private $open;

      public function __construct($toggler_class = 'toggler', $item_class = 'item') {

            $this->items = array();

            $this->toggler_class = $toggler_class;

            $this->item_class = $item_class;
                   $this->open = 0;     

      public function addItem($toggler_content = '',$item_content = '',$open = 0) {

             $this->items[] = array('toggler'=>$toggler_content,'item'=>$item_content,'open'=>(int)$open);

     public function build() {

             foreach($this->items as $index=>$item) {

                                 $out .= '<div class="'.$this->toggler_class.'">'.$item['toggler'].'</div>';

                                 $out .= '<div class="'.$this->item_class.'">'.$item['item'].'</div>';

                                 if($item['open'])  {$this->open = $index;}

                    return $out;

       public function output() {


       public function output_js($options = '') {

                        return 'window.addEvent("domready",function(){ var acc = new Accordion($$(".'.$this->toggler_class.'"),$$(".'.$this->item_class.'"),{display: '.$this->open.'}); })';

       public function addStyle($css = '') {

                        $selectors = ' .toggler{cursor:pointer; font-weight:bold; color:#c00; margin: 0; padding:5px; font-size: 16px; font-family: Arial,sans-serif; }';

                $selectors .=' .toggler:hover{background: #eee;}';

                        $selectors .= $css;

                   return '<style type="text/css">'.$selectors.'</style>';



$ob = new Accordion('toggler_class','item_class');





echo'<script type="text/javascript">'.$ob->output_js().'</script>';

$css = ' .toggler{border: 1px solid #ccc;margin: 1px}';
You can see in action: demo

      No comments.

Display Your FeedBurner Count
Posted on Tue 13th October 2009 4.26PM In tricks&tips

Replace "ID" with your actual FeedBurner. First time we check if the cache file exists or not. If doesn't exists we GET the RSS url, parse the content and retrieve the field 'circulation' with regexp or an object SimpleXMLElement, finally we save the count to a file named after current day. Otherwise if the file cache exists we read from cache and printout. You can view in action

    /* cache path */ 
    $cache_path = 'cache/';

    /* filename cache */
    $rss_file_name = Date('Y-m-d').'-rss.txt';

    /* if file not exists then execute*/
    if(!file_exists($cache_path.$rss_file_name)) {

             $xml = '';
             $content = get($xml);

             //method 1 to extract the count using preg_match
             $subscribers = get_match('/circulation="(.*)"/isU',$content);

             if(subscribers) {

                    $subscribers = number_format($subscribers,0,'','');


                    $count = $subscribers;

             //method 2 to extract the count using class SimpleXMLElement
             $xml = new SimpleXMLElement($content); 

             $c = $xml->feed->entry['circulation'];

             /* with $c DO SOMETHING*/

    //otherwise the file exists then execute
    } else {

            $count = file_get_contents($cache_path.$rss_file_name); 

    /* using regexp for grab the circulation */
    function get_match($pattern,$content) {


       return $matches[1];

    //using cURL
    function get($url) {

             //initialize the cURL session
             $ch = curl_init();

             //set cURL url
             //set cURL to return the date instead of printing it to the browser


             $data = curl_exec($ch);
             //close connection

             //if the data not exists then try again
             if(empty($data)) {

                 return 'Try again. server timeout!';

             //otherwise return the data
             } else { return $data;}

    }//end function

You can view in action

      No comments.

« Previous Entries

Top Of Page