算法

本文介绍了多种数据结构如链表、栈、队列、树等,并深入探讨了排序算法,包括冒泡排序、希尔排序等。此外还提供了二叉树的创建与遍历实例。

数据结构与算法

第一章 内容

数据结构:

  • 链表
  • 队列
  • 哈希

算法:

  • 排序
  • 插入排序
  • 希尔排序
  • 冒泡排序
  • 快速排序
  • 选择排序
  • 堆排序
  • 归并排序
  • 基数排序
  • 查找
  • 顺序查找
  • 折半查找
  • 分块查找
  • 二叉树查找
  • 哈希查找
  • 子串搜索
  • Boyer-Moore算法
  • Horspool算法
  • Sunday算法
  • KMP算法
  • KR算法
  • AC自动机

算法技巧:

  • 贪婪
  • 分治
  • 动态规划
  • 随机化
  • 回溯

第2章 常用排序

$arr = array(
17,
6,
5,
9,
8
);

/*2
5
6
9
8*/

//冒泡排序
function maopao($arr) {
    for($i=1;$len=count($arr); $i<$len; $i++) {
        for($j=$i-1; $j>=0; $j--) {
            if($arr[$i] < $arr[$j]) {
                $tmp = $arr[$i];
                $arr[$i] = $arr[$j];
                $arr[$j] = $tmp;
            } else {
                break;
            }
        }    
    }

    return $arr;
}
function cp($arr) {

    for($i=1,$len=count($arr); $i<$len; $i++) {
        if( $arr[$i] < $arr[$i-1] ) {
            $j = $i-1;
            $tmp = $arr[$i];

            while($j>=0 && $tmp < $arr[$j]) {
                $arr[$j+1] = $arr[$j];
                $j-=1;
            }

            $arr[$j+1] = $tmp;
        }
    }

    return $arr;
}

print_r(cp($arr));
// 希尔排序
for($step = floor(count($arr)/2) ; $step>=1; $step = floor($step/2)) {
    for($i=$step;$i<count($arr);$i++) {
        
        $tmp = $arr[$i];
        
        for($j=$i;$j>=$step;$j-=$step) {
            if($tmp < $arr[$j-$step]) {
                $arr[$j] = $arr[$j-$step];
            } else {
                break;
            }
        }

        $arr[$j] = $tmp;
    }
}

二叉树

http://blog.devtang.com/blog/2015/06/16/talk-about-tech-interview/

class Node {
    protected $value = null;
    protected $left = null;
    protected $right = null;

    public function insert($value) {
        if($this->value === null) {
            $this->value = $value;
        } else if($value <= $this->value) {
            if($this->left === null) {
                $this->left =new self();
                $this->left->insert($value);
            } else {
                $this->left->insert($value);
            }
        } else if($value > $this->value) {
            if($this->right === null) {
                $this->right =new self();
                $this->right->insert($value);
            } else {
                $this->right->insert($value);
            }
        }
    }
}


$tree = new Node();

$tree->insert(4);
$tree->insert(2);
$tree->insert(1);
$tree->insert(3);
$tree->insert(6);
$tree->insert(5);
$tree->insert(7);

print_r($tree);
// 遍历
function bianli($tree) {
    if($tree->left!==null) {
         bianli($tree->left);
    }
    
    echo $tree->value , '<br />';

    if($tree->right!==null) {
        bianli($tree->right);
    }       
}


bianli($tree);
//反转二叉树
function revTree($tree) {
    if($tree->left !== null) {
        $tree->left = revTree($tree->left);
    }

    if($tree->right !== null) {
        $tree->right = revTree($tree->right);
    }

    $tmp = $tree->left;
    $tree->left = $tree->right;
    $tree->right = $tmp;

    return $tree;
}

print_r( revTree($tree) );
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

银色种子

打赏 >100 请留言,并私信

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值