Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

quicksort

<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php

function quick_sort($array){
    
    //first find the size of the array
    $length  =  count($array);
    
    if($length <= 1) {
        
        return $array;
    }
    else {
        
            //find that pivot dawg
            $pivot = $array[0];
            $left = $right = array();
            
            
            //loop through all items in array group into left and right arrays
            for($i = 1; $i < count($array); $i++) {
                
                
                if($array[$i] < $pivot) {
                    
                    $left[] =  $array[$i];
    
                }
                
                else {
                    
                    $right[] =  $array[$i];
                }
                
                
            }
            
            
            //recursively combine the results to get final sorted array
            return array_merge(quick_sort($left), array($pivot), quick_sort($right));
        
        
    }
    
}
print_r(quick_sort(array(43)));
?>
</body>
</html>

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.