Home > Article > Backend Development > Such profound knowledge~ Sorting can be achieved using only two stacks
$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.