简明现代魔法 -> PHP服务器脚本 -> 一道冒泡排序的算法面试题

一道冒泡排序的算法面试题

2011-06-11

一道PHP笔试题:请使用伪语言结合数据结构冒泡排序法对以下一组数据进行排序 10 2 36 14 10 25 23 85 99 45。

关于冒泡排序大家应该都非常熟悉了,原理就不多说了,这里只做简单记录。下面是参考代码。

<?php
class engage
{
    public function getArray()
    {
        $arr1 = array (
			'0' => array ('fid' => 1, 'tid' => 1, 'name' => 'Name1' ),
			'1' => array ('fid' => 1, 'tid' => 2 , 'name' => 'Name2' ),
			'2' => array ('fid' => 1, 'tid' => 5 , 'name' => 'Name3' ),
			'3' => array ('fid' => 1, 'tid' => 7 , 'name' => 'Name4' ),
			'4' => array ('fid' => 3, 'tid' => 9, 'name' => 'Name5' )
        );
        $arr2 = array();
        foreach ($arr1 as $key => $value)
        {
            $arr2[$value['fid']][] = array('tid' => $value['tid'],'name' => $value['name']);
        }
        return $arr2;
    }
    //print_r($arr2);
    //$eString = 'open_door';
    public function getString($eString = null)
    {
        $eString = explode('_',$eString);
        $eString = array_map("ucfirst",$eString);
        $eString = implode($eString,'');
        return  $eString;
    }
    /**
     * 冒泡排序 
     * @return unknown
     */
    public function getBubble()
    {
        $isOver = false;
        $bubbleArray =  array(10,2,36,14,10,25,23,85,99,45);
        $bubbleResult = $bubbleArray;
        do{
            $bubbleArray = $bubbleResult;
            $isOver = true;
            foreach ($bubbleArray as $key => $value)
            {
                if ($value < $bubbleResult[$key-1])
                {
                    $bubbleResult[$key]=$bubbleResult[$key-1];
                    $bubbleResult[$key-1]=$value;
                    $isOver = false;
                }
            }
        }while (!$isOver);
        return $bubbleResult;
    }

}

$engage = new engage();
echo '<pre>';
print_r($engage->getArray());
echo '</pre>';

echo $engage->getString('make_by_nowamagic');
echo '<pre>';
print_r($engage->getBubble());
echo '</pre>';
?>

程序运行结果如下:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [tid] => 1
                    [name] => Name1
                )

            [1] => Array
                (
                    [tid] => 2
                    [name] => Name2
                )

            [2] => Array
                (
                    [tid] => 5
                    [name] => Name3
                )

            [3] => Array
                (
                    [tid] => 7
                    [name] => Name4
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [tid] => 9
                    [name] => Name5
                )

        )

)
MakeByNowamagic
Array
(
    [0] => 2
    [1] => 10
    [2] => 10
    [3] => 14
    [4] => 23
    [5] => 25
    [6] => 36
    [7] => 45
    [8] => 85
    [9] => 99
)
随机文章推荐
网站分类


注:如需转载本文,请注明出处(原文链接),谢谢。更多精彩内容,请进入简明现代魔法首页。

进入新博客
喜欢本文,就分享它吧
给我留言
您的名字:
您的邮件:
您的网站:


 

copyright © 2009 简明现代魔法    学习、分享、进步

power by Gonn 感谢所有关心和支持本站的朋友们