Home > Backend Development > PHP Tutorial > Examples of php bubble sort and quick sort

Examples of php bubble sort and quick sort

WBOY
Release: 2016-07-25 09:03:29
Original
980 people have browsed it
+ +$i)
{
for($j=$len; $j>$i; --$j)
    {
  1. if($array[$j] < $array[$j-1])
  2. {
  3. $temp = $array[$j];
  4. $array[$j] = $array[$j-1];
  5. $array[$j-1] = $temp;
  6. }
  7. }
  8. }
  9. return $ array;
  10. }

  11. print '

    ';</li>
    <li>print_r(bubbingSort(array(1,4,22,5,7,6,9)));</li>
    <li>print '&lt ;/pre>';</li>
    <li>?></p></li>
    <li>
    <li>
    <li>
    <li>Copy code</li>
    <li>
    <li>
    <li>
    <li>
    <li>implementation principle of quick sort
    </li>
    <li>
    </ol>First make sure that the first half of the list is smaller than the second half, and then sort the first half and the second half respectively, so that the entire list is in order.
     </div>
    <em onclick="copycode($('code_L6B'));"></em>
    </div>
    <p><u><strong><p><?php</strong>function quickSort(array $array)</u>{</p>$len = count($array);<div class="blockcode">if($len <= 1)<div id="code_Ayu">{<ol>return $ array;<li>}</li>
    <li>$key = $array[0];</li>
    <li>$left = array();</li>
    <li>$right = array();</li>
    <li>for($i=1; $i<$len; ++$i)</li>
    <li> {</li>
    <li>if($array[$i] < $key)</li>
    <li>{</li>
    <li>$left[] = $array[$i];</li>
    <li>}</li>
    <li>else</li>
    <li>{</li>
    <li>$right[] = $array[$i];</li>
    <li> }</li>
    <li>}</li>
    <li>$left = quickSort($left);</li>
    <li>$right = quickSort($right);</li>
    <li>return array_merge($left, array($key), $right);</li>
    <li>}</p></li>
    <li>< p>print '<pre class="brush:php;toolbar:false">';</li>
    <li>print_r(quickSort(array(1,4,22,5,7,6,9)));</li>
    <li>print '
    ';
  12. ?>< /p>
  13. Copy code
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