search
HomeBackend DevelopmentPHP TutorialAlgorithm analysis of PHP interview questions, PHP test question analysis_PHP tutorial

Algorithm analysis of PHP interview questions, PHP test question analysis

In interviews, we are often asked what algorithms we know. Here are some common algorithms and their implementation principles. The following examples They are all tested and available. If you have any questions, please let us know!!

I am a novice, if there is a better way to achieve it, please enlighten me, I will be grateful!!!!

Bubble sort, quick sort, selection sort, binary search, quick search

<span>/*<span>* 
* 冒泡排序
* 相邻2数比较,小的在前,大的在后
* 数组有几个元素,就要比较几轮 $i
* 每轮需要比较的次数为,数组元素个数-已比较的次数 $j
* @param   array  <span><span>$array 要操作的数组</span></span>
* @return  array  <span><span>$array 返回的数组</span></span>
<span>*/
<span>function bubbleSort<span>(</span><span>$array<span>)
{
        <span>$cnt = <span>count<span>(</span><span>$array<span>);
        <span>for(<span>$i = 0; <span>$i < <span>$cnt ; <span>$i++<span>){
                <span>for(<span>$j = 0 ; <span>$j < (<span>$cnt-<span>$i-1) ; <span>$j++<span>){
                        <span>if(<span>$array[<span>$j] > <span>$array[<span>$j+1<span>]){
                                <span>$temp = <span>$array[<span>$j<span>];
                                <span>$array[<span>$j] = <span>$array[<span>$j+1<span>];
                                <span>$array[<span>$j+1] = <span>$temp<span>;
                        }
                }
        }
        <span>return <span>$array<span>;
}<br /></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<span>/*</span><span>*
* 快速排序
* 递归实现
* 获取数组第一个数,循环使后面的数与其比较,
* 比其小的放在一个数组中,比其大的放在一个数组中
* 将2个数组递归调用,直到最终数组元素小于等于1时,没有可以比较的元素
* 通过array_merge函数,将比较的数组按大小顺序合并然后一层一层的return出去,最终实现从小到大排序
* @param array $array 要操作的数组
* @return array $array 返回的数组
</span><span>*/</span>

<span>function</span> quickSort(<span>$array</span><span>)
{
        </span><span>if</span>(<span>count</span>(<span>$array</span>) <= 1 ) <span>return</span> <span>$array</span><span>;
        </span><span>$key</span> = <span>$array</span>[0<span>];
        </span><span>$left_arr</span> = <span>array</span><span>();
        </span><span>$right_arr</span> = <span>array</span><span>();
        </span><span>for</span> (<span>$i</span>=1;<span>$i</span><<span>count</span>(<span>$array</span>);<span>$i</span>++<span>){
                </span><span>if</span>(<span>$array</span>[<span>$i</span>] <= <span>$key</span><span>){
                        </span><span>$left_arr</span>[] = <span>$array</span>[<span>$i</span><span>];
                }</span><span>else</span><span>{
                        </span><span>$right_arr</span>[] = <span>$array</span>[<span>$i</span><span>];
                }
        }

        </span><span>$left_arr</span> = quickSort(<span>$left_arr</span><span>);
        </span><span>$right_arr</span> = quickSort(<span>$right_arr</span><span>);
        </span><span>return</span> <span>array_merge</span>(<span>$left_arr</span>,<span>array</span>(<span>$key</span>),<span>$right_arr</span><span>);

}</span>

<span>/*</span><span>*
* 选择排序
* 2层循环
* 第一层逐个获取数组的值 $array[$i]
* 第二次遍历整个数组与$array[$i]比较($j=$i+1已经比较的,不再比较,减少比较次数)
* 如果比$array[$i]小,就交换位置
* 这样一轮下来就可以得到数组中最小值
* 以此内推整个外层循环下来就数组从小到大排序了
* @param array $array 要比较的数组
* @return array $array 从小到大排序后的数组
</span><span>*/</span>

<span>function</span> selectSort(<span>$array</span><span>){
        </span><span>$cnt</span> = <span>count</span>(<span>$array</span><span>);
        </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$cnt</span>;<span>$i</span>++<span>){
                </span><span>for</span>(<span>$j</span>=(<span>$i</span>+1);<span>$j</span><<span>$cnt</span>;<span>$j</span>++<span>){
                        </span><span>if</span>(<span>$array</span>[<span>$i</span>]><span>$array</span>[<span>$j</span><span>]){
                                </span><span>$tmp</span> = <span>$array</span>[<span>$i</span><span>];
                                </span><span>$array</span>[<span>$i</span>] = <span>$array</span>[<span>$j</span><span>];
                                </span><span>$array</span>[<span>$j</span>] = <span>$tmp</span><span>;
                        }
                }
        }
        </span><span>return</span> <span>$array</span><span>;
}</span>

<span>/*</span><span>*
* 二分法查找一个值在数组中的位置
* @param array $array 操作的数组
* @param void $val 要查找的值
* @return int $mid 返回要查找的值在数组中的索引,如果不存在返回-1
</span><span>*/</span>

<span>function</span> binarySearch(<span>$array</span>,<span>$val</span><span>)
{
        </span><span>$cnt</span> = <span>count</span>(<span>$array</span><span>);
        </span><span>$low</span> = 0<span>;
        </span><span>$high</span> = <span>$cnt</span> - 1<span>;
        </span><span>while</span> (<span>$low</span> <= <span>$high</span><span>){
                </span><span>$mid</span> = <span>intval</span>((<span>$low</span> + <span>$high</span>)/2<span>);
                </span><span>if</span>(<span>$array</span>[<span>$mid</span>] == <span>$val</span><span>){
                        </span><span>return</span> <span>$mid</span><span>;
                }

                </span><span>if</span>(<span>$array[$mid]</span> < <span>$val</span><span>){
                        </span><span>$low</span> = <span>$mid</span> + 1<span>;
                }

                </span><span>if</span>(<span>$array[$mid]</span> > <span>$val</span><span>){
                        </span><span>$high</span> = <span>$mid</span> - 1<span>;
                }
        }

        </span><span>return</span> -1<span>;
}</span>

<span>/*</span><span>*
* 顺序查找(最简单,效率低下)
* 通过循环数组查找要的值
* @param array $array 要操作的数组
* @param void $val  要查找的值
* @return int 如果存在,返回该值在数组中的索引,否则返回-1
</span><span>*/</span>

<span>function</span> seqSch(<span>$array</span>,<span>$val</span><span>)
{
        </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$array</span>);<span>$i</span>++<span>){
                </span><span>if</span>(<span>$array</span>[<span>$i</span>] == <span>$val</span><span>)
                        </span><span>break</span><span>;
        }

        </span><span>if</span>(<span>$i</span> < <span>count</span>(<span>$array</span><span>)){
                </span><span>return</span> <span>$i</span><span>;
        }</span><span>else</span><span>{
                </span><span>return</span> -1<span>;
        }
}</span>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1059464.htmlTechArticleAlgorithm Analysis of PHP Interview Questions. What algorithms are often asked in PHP test question analysis interviews. Here are some common ones. Algorithms and their implementation principles. The following examples are all tested and available...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
2023年前端React面试题大汇总(收藏)2023年前端React面试题大汇总(收藏)Aug 04, 2020 pm 05:33 PM

php中文网作为知名编程学习网站,为您整理了一些React面试题,帮助前端开发人员准备和清除React面试障碍。

五个常见的Go语言面试题及解答五个常见的Go语言面试题及解答Jun 01, 2023 pm 08:10 PM

作为近年来备受热捧的一门编程语言,Go语言已经成为众多公司与企业的面试热点。对于Go语言初学者而言,在面试过程中遇到相关问题时,如何回答是一个值得探讨的问题。下面列举五个常见的Go语言面试题及解答,供初学者参考。请介绍一下Go语言的垃圾回收机制是如何工作的?Go语言的垃圾回收机制基于标记-清除算法和三色标记算法。当Go程序中的内存空间不够用时,Go垃圾回收器

2023年精选Web前端面试题大全及答案(收藏)2023年精选Web前端面试题大全及答案(收藏)Apr 08, 2021 am 10:11 AM

本篇文章给大家总结一些值得收藏的精选Web前端面试题(附答案)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

50个你必须掌握的Angular面试题(收藏)50个你必须掌握的Angular面试题(收藏)Jul 23, 2021 am 10:12 AM

本篇文章给大家分享50个必须掌握的Angular面试题,会从初学者-中级-高级三个部分来解析这50个面试题,带大家吃透它们!

面试官:你对高并发了解多少?我:emmm...面试官:你对高并发了解多少?我:emmm...Jul 26, 2023 pm 04:07 PM

高并发,几乎是每个程序员都想拥有的经验。原因很简单:随着流量变大,会遇到各种各样的技术问题,比如接口响应超时、CPU load升高、GC频繁、死锁、大数据量存储等等,这些问题能推动我们在技术深度上不断精进。

2023年vue高频面试题分享(附答案分析)2023年vue高频面试题分享(附答案分析)Aug 01, 2022 pm 08:08 PM

本篇文章给大家总结一些值得收藏的2023年精选vue高频面试题(附答案)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

总结一些前端常见面试题(附答案),带你巩固知识点!总结一些前端常见面试题(附答案),带你巩固知识点!Jul 29, 2022 am 09:49 AM

发布文章主要也是巩固自己的知识更加熟练,全凭自己的理解和网上查资料总结出来的,如有不对的地方还望多多指点。下面是我总结的一下常见面试题,为了督促自己还可以会继续更新

看看这些前端面试题,带你搞定高频知识点(四)看看这些前端面试题,带你搞定高频知识点(四)Feb 20, 2023 pm 07:19 PM

每天10道题,100天后,搞定所有前端面试的高频知识点,加油!!!,在看文章的同时,希望不要直接看答案,先思考一下自己会不会,如果会,自己的答案是什么?想过之后再与答案比对,是不是会更好一点,当然如果你有比我更好的答案,欢迎评论区留言,一起探讨技术之美。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment