Home  >  Article  >  Backend Development  >  Such profound knowledge~ Sorting can be achieved using only two stacks

Such profound knowledge~ Sorting can be achieved using only two stacks

WBOY
WBOYOriginal
2016-07-29 09:05:141648browse
$stackA = range(1,9);
shuffle($stackA);
print_r($stackA);
$stackB = array();
while(count($stackA)){
    $tmpA = array_shift($stackA);
    while(count($stackB)){
        $tmpB = array_shift($stackB);
        if($tmpB>=$tmpA){
            array_unshift($stackB,$tmpB);
            break;
        }else{
            array_unshift($stackA,$tmpB);
        }
    }
    array_unshift($stackB,$tmpA);
}
print_r($stackB);

Yeah, LZ has been thinking about it for five years and finally realized it today hahahaha~

The above has introduced a lot of profound knowledge~ Sorting can be achieved using only two stacks, including all aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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