Home Backend Development PHP Tutorial Such profound knowledge~ Sorting can be achieved using only two stacks

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

Jul 29, 2016 am 09:05 AM
array

$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);
Copy after login

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Sort array using Array.Sort function in C#

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

Simple and clear method to use PHP array_merge_recursive() function

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

How to use the array_combine function in PHP to combine two arrays into an associative array

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

Detailed explanation of PHP array_fill() function usage

How to use the Array module in Python How to use the Array module in Python May 01, 2023 am 09:13 AM

How to use the Array module in Python

What are the common causes of ArrayStoreException in Java? What are the common causes of ArrayStoreException in Java? Jun 25, 2023 am 09:48 AM

What are the common causes of ArrayStoreException in Java?

Introduction to how to use the PHP array_change_key_case() function Introduction to how to use the PHP array_change_key_case() function Jun 27, 2023 am 10:43 AM

Introduction to how to use the PHP array_change_key_case() function

What are the common causes of ArrayIndexOutOfBoundsException in Java? What are the common causes of ArrayIndexOutOfBoundsException in Java? Jun 24, 2023 pm 10:39 PM

What are the common causes of ArrayIndexOutOfBoundsException in Java?

See all articles