php-logic-1

WBOY
Freigeben: 2016-08-08 09:20:16
Original
1099 Leute haben es durchsucht

比较这两个排序算法,实现有序数组的重新排序
1 . 定义$a,$b两个指针
2 . 使用php的内置函数
<code><span><span><?php </span><span>$array_a</span> = <span>array</span>(<span>2</span>,<span>5</span>,<span>7</span>);
    <span>$array_b</span> = <span>array</span>(<span>1</span>,<span>2</span>,<span>3</span>,<span>8</span>,<span>9</span>);
    <span>$length_a</span> = count(<span>$array_a</span>);
    <span>$length_b</span> = count(<span>$array_b</span>);
    <span>$array_c</span> = <span>array</span>();
    <span>$a</span> = <span>0</span>;
    <span>$b</span> = <span>0</span>;
    <span>$time_a</span> = microtime(<span>true</span>);
    <span>while</span>(<span>true</span>){
        <span>if</span>(<span>$a</span> == <span>$length_a</span> || <span>$b</span> == <span>$length_b</span>)
            <span>break</span>;
        <span>if</span>(<span>$array_a</span>[<span>$a</span>] $array_b</span>[<span>$b</span>]){
            <span>$array_c</span>[] = <span>$array_a</span>[<span>$a</span>];
            ++<span>$a</span>;
        }<span>else</span>{
            <span>$array_c</span>[] = <span>$array_b</span>[<span>$b</span>];
            ++<span>$b</span>;
        }
    }
    <span>if</span>(<span>$a</span> == <span>$length_a</span>){
        <span>while</span>(<span>$b</span> $length_b</span>){
            <span>$array_c</span>[] = <span>$array_b</span>[<span>$b</span>++];
        }
    }
    <span>else</span>{
        <span>while</span>(<span>$a</span> $length_a){
            <span>$array_c</span>[] = <span>$array_a</span>[<span>$a</span>++];
        }
    }
    <span>//var_dump($array_c);</span><span>$result_a</span> = microtime(<span>true</span>) - <span>$time_a</span>;

<span>//2.</span><span>$time_b</span> = microtime(<span>true</span>);
    <span>$array_c</span> = <span>array</span>();
    <span>$array_c</span> = array_merge(<span>$array_a</span>, <span>$array_b</span>);
    sort(<span>$array_c</span>);
    <span>//var_dump($array_c);</span><span>$result_b</span> = microtime(<span>true</span>) - <span>$time_b</span>;

    <span>echo</span> (<span>$result_b</span> - <span>$result_a</span>) > <span>0</span> ? <span>1</span> : <span>0</span>; 
<span>?></span></code>
Nach dem Login kopieren

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了php-logic-1,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage