search
Homephp教程php手册php bubble sort quick sort

php bubble sort quick sort

Jul 06, 2016 pm 01:30 PM
phpexchangebubblefastsortnumerical value

/****** 1) Bubble sorting: Exchange values ​​in pairs, with the smallest value on the far left, just like the lightest bubble on the top. 2) Exchange the entire column of numbers once, with the smallest number on the far left. Each time you can get the smallest number among the remaining numbers. The emerging numbers form an ordered interval, and the remaining values ​​form An unordered interval, and the value of each element in the ordered interval is greater than nothing

/******
1) Bubble sort: Exchange values ​​in pairs, with the smallest value on the left, just like the lightest bubble on the top.
2) Exchange the entire column of numbers once, with the smallest number on the far left. Each time, you can get the smallest number among the remaining numbers. The "popped" numbers form an ordered interval, and the remaining numbers are The values ​​below form an unordered interval, and the value of each element in the ordered interval is smaller than that of the unordered interval.
3) Quick sort: base number, left and right arrays, recursive call, merge.
4) Insertion sort: The sorting interval is divided into two parts, the left side is ordered and the right side is unordered. Take the first element from the right interval and insert it into the left interval. If this element is larger than the rightmost element of the left interval, leave it where it is. If this element is smaller than the rightmost element in the left range, it will be inserted at the original position of the rightmost element. At the same time, the rightmost element will be shifted one position to the right. The calculator will be reduced by one and will be compared with the previous element again until the previous element is smaller than the previous element. To insert elements as small as possible, repeat the above steps.
6) Pay attention to the processing of interval endpoint values, and the subscript of the first element of the array is 0.
***/

<span style="color: #800080;"><br>$a</span>=<span style="color: #0000ff;">array</span>('3','8','1','4','11','7'<span style="color: #000000;">);
</span><span style="color: #008080;">PRint_r</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">);
</span><span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">从小到大</span>
<span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1;<span style="color: #800080;">$i</span>$len;<span style="color: #800080;">$i</span>++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$len</span>-1;<span style="color: #800080;">$j</span>>=<span style="color: #800080;">$i</span>;<span style="color: #800080;">$j</span>--<span style="color: #000000;">)
</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>]$a[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">])
{</span><span style="color: #008000;">//</span><span style="color: #008000;">如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了</span>
 <span style="color: #800080;">$x</span>=<span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
 </span><span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>]=<span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">];
 </span><span style="color: #800080;">$a</span>[<span style="color: #800080;">$j</span>-1]=<span style="color: #800080;">$x</span><span style="color: #000000;">;
}
}
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">另一种方法 从小到大</span>
<span style="color: #800080;">$b</span>=<span style="color: #0000ff;">array</span>('4','3','8','9','2','1'<span style="color: #000000;">);
</span><span style="color: #800080;">$len</span>=<span style="color: #008080;">count</span>(<span style="color: #800080;">$b</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$k</span>=1;<span style="color: #800080;">$k</span>$len;<span style="color: #800080;">$k</span>++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$len</span>-1,<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$len-<span style="color: #800080;">$k</span>;<span style="color: #800080;">$i</span>++,<span style="color: #800080;">$j</span>--<span style="color: #000000;">)
</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>]$b[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">]){
</span><span style="color: #008000;">//</span><span style="color: #008000;">如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了</span>
 <span style="color: #800080;">$tmp</span>=<span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
 </span><span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>]=<span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">];
 </span><span style="color: #800080;">$b</span>[<span style="color: #800080;">$j</span>-1]=<span style="color: #800080;">$tmp</span><span style="color: #000000;">;
}
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$b</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "
"<span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">下面的这个执行效率更高</span>
<span style="color: #0000ff;">function</span> maopao(<span style="color: #800080;">$arr</span><span style="color: #000000;">)
{
 </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);
 </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1; <span style="color: #800080;">$i</span>$len; <span style="color: #800080;">$i</span>++)<span style="color: #008000;">//</span><span style="color: #008000;">最多做n-1趟排序</span>
<span style="color: #000000;"> {
  </span><span style="color: #800080;">$flag</span> = <span style="color: #0000ff;">false</span>;    <span style="color: #008000;">//</span><span style="color: #008000;">本趟排序开始前,交换标志应为假</span>
  <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$len</span>-1;<span style="color: #800080;">$j</span>>=<span style="color: #800080;">$i</span>;<span style="color: #800080;">$j</span>--<span style="color: #000000;">)
  {
   </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>]$arr[<span style="color: #800080;">$j</span>-1])<span style="color: #008000;">//</span><span style="color: #008000;">交换记录</span>
   {<span style="color: #008000;">//</span><span style="color: #008000;">如果是从大到小的话,只要在这里的判断改成if($arr[$j]>$arr[$j-1])就可以了</span>
     <span style="color: #800080;">$x</span>=<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
     </span><span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>]=<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">];
     </span><span style="color: #800080;">$arr</span>[<span style="color: #800080;">$j</span>-1]=<span style="color: #800080;">$x</span><span style="color: #000000;">;
     </span><span style="color: #800080;">$flag</span> = <span style="color: #0000ff;">true</span>;<span style="color: #008000;">//</span><span style="color: #008000;">发生了交换,故将交换标志置为真</span>
<span style="color: #000000;">   }
  }
  </span><span style="color: #0000ff;">if</span>(! <span style="color: #800080;">$flag</span>)<span style="color: #008000;">//</span><span style="color: #008000;">本趟排序未发生交换,提前终止算法</span>
  <span style="color: #0000ff;">return</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;   
 }
}
</span><span style="color: #800080;">$shuz</span> = <span style="color: #0000ff;">array</span>('2','4','1','8','5'<span style="color: #000000;">);
</span><span style="color: #800080;">$bb</span> = maopao(<span style="color: #800080;">$shuz</span><span style="color: #000000;">);
</span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$bb</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 快速排序</span>
<span style="color: #0000ff;">function</span> kuaisu(<span style="color: #800080;">$arr</span><span style="color: #000000;">){
    </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$len</span> ){
        <span style="color: #0000ff;">return</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;
    }
    </span><span style="color: #800080;">$key</span> = <span style="color: #800080;">$arr</span>[0<span style="color: #000000;">];
    </span><span style="color: #800080;">$left_arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #800080;">$right_arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1; <span style="color: #800080;">$i</span>$len;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span>] $key<span style="color: #000000;">){
            </span><span style="color: #800080;">$left_arr</span>[] = <span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #800080;">$right_arr</span>[] = <span style="color: #800080;">$arr</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
        }
    }
    </span><span style="color: #800080;">$left_arr</span> = kuaisu(<span style="color: #800080;">$left_arr</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$right_arr</span> = kuaisu(<span style="color: #800080;">$right_arr</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$left_arr</span>, <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$key</span>), <span style="color: #800080;">$right_arr</span><span style="color: #000000;">);
}
</span><span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span>(23,98,54,2,9,62,34<span style="color: #000000;">);
</span><span style="color: #008080;">print_r</span>(kuaisu(<span style="color: #800080;">$arr</span>));

 


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools