Skip to main content
Formatting fix
Source Link
Blrfl
  • 20.5k
  • 2
  • 52
  • 76

How would I express the speed of the following sort algorithm? I know bubblesort is n^n (er, I mean n^2...as someone pointed out below). In the following the array decreases each time you call it recursively.

thanks.


$myarray = array("21","4","8","8","1","2","19","21"); mysort($myarray);

$myarray = array("21","4","8","8","1","2","19","21");
mysort($myarray);

function mysort($myarray,$sorted = null){
    if(!isset($sorted)){
        $sorted = array();
    }
    $lowest = $myarray[0];
    $lowindex = 0;
    for($x = 1;$x < count($myarray);$x++){
        if($myarray[$x] < $lowest){
            $lowest = $myarray[$x];
            $lowindex = $x;
        }
    }
    $sorted[] = $lowest;
    array_splice($myarray,$lowindex,1);
    if(count($myarray) > 1){
        echo "calling recursively \n";
        mysort($myarray,$sorted);
    }
    else if(count($myarray) == 1){      
        $sorted[] = $myarray[0];
        $arraystr = print_r($sorted,true);
        echo "sorting finished. Array is $arraystr \n";
        return;
    }
}

How would I express the speed of the following sort algorithm? I know bubblesort is n^n (er, I mean n^2...as someone pointed out below). In the following the array decreases each time you call it recursively.

thanks.


$myarray = array("21","4","8","8","1","2","19","21"); mysort($myarray);

function mysort($myarray,$sorted = null){
    if(!isset($sorted)){
        $sorted = array();
    }
    $lowest = $myarray[0];
    $lowindex = 0;
    for($x = 1;$x < count($myarray);$x++){
        if($myarray[$x] < $lowest){
            $lowest = $myarray[$x];
            $lowindex = $x;
        }
    }
    $sorted[] = $lowest;
    array_splice($myarray,$lowindex,1);
    if(count($myarray) > 1){
        echo "calling recursively \n";
        mysort($myarray,$sorted);
    }
    else if(count($myarray) == 1){      
        $sorted[] = $myarray[0];
        $arraystr = print_r($sorted,true);
        echo "sorting finished. Array is $arraystr \n";
        return;
    }
}

How would I express the speed of the following sort algorithm? I know bubblesort is n^n (er, I mean n^2...as someone pointed out below). In the following the array decreases each time you call it recursively.

thanks.


$myarray = array("21","4","8","8","1","2","19","21");
mysort($myarray);

function mysort($myarray,$sorted = null){
    if(!isset($sorted)){
        $sorted = array();
    }
    $lowest = $myarray[0];
    $lowindex = 0;
    for($x = 1;$x < count($myarray);$x++){
        if($myarray[$x] < $lowest){
            $lowest = $myarray[$x];
            $lowindex = $x;
        }
    }
    $sorted[] = $lowest;
    array_splice($myarray,$lowindex,1);
    if(count($myarray) > 1){
        echo "calling recursively \n";
        mysort($myarray,$sorted);
    }
    else if(count($myarray) == 1){      
        $sorted[] = $myarray[0];
        $arraystr = print_r($sorted,true);
        echo "sorting finished. Array is $arraystr \n";
        return;
    }
}
added 21 characters in body
Source Link
nettie
  • 111
  • 5

How would I express the speed of the following sort algorithm? I know bubblesort is n^n (er, I mean n^2...as someone pointed out below). In the following the array decreases each time you call it recursively. Would that make it n^(n-1)?

thanks.


$myarray = array("21","4","8","8","1","2","19","21"); mysort($myarray);

function mysort($myarray,$sorted = null){
    if(!isset($sorted)){
        $sorted = array();
    }
    $lowest = $myarray[0];
    $lowindex = 0;
    for($x = 1;$x < count($myarray);$x++){
        if($myarray[$x] < $lowest){
            $lowest = $myarray[$x];
            $lowindex = $x;
        }
    }
    $sorted[] = $lowest;
    array_splice($myarray,$lowindex,1);
    if(count($myarray) > 1){
        echo "calling recursively \n";
        mysort($myarray,$sorted);
    }
    else if(count($myarray) == 1){      
        $sorted[] = $myarray[0];
        $arraystr = print_r($sorted,true);
        echo "sorting finished. Array is $arraystr \n";
        return;
    }
}

How would I express the speed of the following sort algorithm? I know bubblesort is n^n. In the following the array decreases each time you call it recursively. Would that make it n^(n-1)?

thanks.


$myarray = array("21","4","8","8","1","2","19","21"); mysort($myarray);

function mysort($myarray,$sorted = null){
    if(!isset($sorted)){
        $sorted = array();
    }
    $lowest = $myarray[0];
    $lowindex = 0;
    for($x = 1;$x < count($myarray);$x++){
        if($myarray[$x] < $lowest){
            $lowest = $myarray[$x];
            $lowindex = $x;
        }
    }
    $sorted[] = $lowest;
    array_splice($myarray,$lowindex,1);
    if(count($myarray) > 1){
        echo "calling recursively \n";
        mysort($myarray,$sorted);
    }
    else if(count($myarray) == 1){      
        $sorted[] = $myarray[0];
        $arraystr = print_r($sorted,true);
        echo "sorting finished. Array is $arraystr \n";
        return;
    }
}

How would I express the speed of the following sort algorithm? I know bubblesort is n^n (er, I mean n^2...as someone pointed out below). In the following the array decreases each time you call it recursively.

thanks.


$myarray = array("21","4","8","8","1","2","19","21"); mysort($myarray);

function mysort($myarray,$sorted = null){
    if(!isset($sorted)){
        $sorted = array();
    }
    $lowest = $myarray[0];
    $lowindex = 0;
    for($x = 1;$x < count($myarray);$x++){
        if($myarray[$x] < $lowest){
            $lowest = $myarray[$x];
            $lowindex = $x;
        }
    }
    $sorted[] = $lowest;
    array_splice($myarray,$lowindex,1);
    if(count($myarray) > 1){
        echo "calling recursively \n";
        mysort($myarray,$sorted);
    }
    else if(count($myarray) == 1){      
        $sorted[] = $myarray[0];
        $arraystr = print_r($sorted,true);
        echo "sorting finished. Array is $arraystr \n";
        return;
    }
}
Source Link
nettie
  • 111
  • 5

expressing the speed of a sorting algorithm

How would I express the speed of the following sort algorithm? I know bubblesort is n^n. In the following the array decreases each time you call it recursively. Would that make it n^(n-1)?

thanks.


$myarray = array("21","4","8","8","1","2","19","21"); mysort($myarray);

function mysort($myarray,$sorted = null){
    if(!isset($sorted)){
        $sorted = array();
    }
    $lowest = $myarray[0];
    $lowindex = 0;
    for($x = 1;$x < count($myarray);$x++){
        if($myarray[$x] < $lowest){
            $lowest = $myarray[$x];
            $lowindex = $x;
        }
    }
    $sorted[] = $lowest;
    array_splice($myarray,$lowindex,1);
    if(count($myarray) > 1){
        echo "calling recursively \n";
        mysort($myarray,$sorted);
    }
    else if(count($myarray) == 1){      
        $sorted[] = $myarray[0];
        $arraystr = print_r($sorted,true);
        echo "sorting finished. Array is $arraystr \n";
        return;
    }
}