php-logic-1

WBOY
Release: 2016-08-08 09:20:16
Original
1088 people have browsed it

Compare these two sorting algorithms to implement reordering of ordered arrays
1. Define two pointers$a,$b
2. Use PHP’s built-in function
<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>] <= <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> < <span>$length_b</span>){
            <span>$array_c</span>[] = <span>$array_b</span>[<span>$b</span>++];
        }
    }
    <span>else</span>{
        <span>while</span>(<span>$a</span> < <span>$length_a</span>){
            <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></span></span></code>
Copy after login

Copyright Disclaimer: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced php-logic-1, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template