Table of Contents
php 冒泡排序 快速排序,php冒泡排序
Home php教程 php手册 php 冒泡排序 快速排序,php冒泡排序

php 冒泡排序 快速排序,php冒泡排序

Jun 13, 2016 am 08:49 AM
Bubble Sort

php 冒泡排序 快速排序,php冒泡排序

/******
1)冒泡排序:两两交换数值,最小的值在最左边,就如最轻的气泡在最上边。
2)对整列数两两交换一次,最小的数在最左边,每次都能得一个在剩下的数中的最小 的数,“冒”出来的数组成一个有序区间,剩下的值组成一无序区间,且有序区间中每一元素值都比无序区间的小。
3)快速排序:基准数,左右二个数组,递归调用,合并。
4)插入排序:排序区间分成二部分,左边有序,右边无序,从右区间取第一个元素插入左区间,若此元素比左边区间最右边的元素大,留在原处,若此元素比左 边区间最右边的元素小,则插在最右边5)元素的原位置,同时最右边元素右移一位,计算器减一,重新和前面的元素比较,直到前面的元素比要插入元素小为止,重复 上述步骤。
6)注意区间端点值的处理,及数组的第一个元素下标为0.
***/

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

 

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Java data structures and algorithms: in-depth explanation Java data structures and algorithms: in-depth explanation May 08, 2024 pm 10:12 PM

Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

Transform code with C++ function pointers: improve efficiency and reusability Transform code with C++ function pointers: improve efficiency and reusability Apr 29, 2024 pm 06:45 PM

Function pointer technology can improve code efficiency and reusability, specifically as follows: Improved efficiency: Using function pointers can reduce repeated code and optimize the calling process. Improve reusability: Function pointers allow the use of general functions to process different data, improving program reusability.

Guide to writing a custom sorting algorithm for PHP arrays Guide to writing a custom sorting algorithm for PHP arrays Apr 27, 2024 pm 06:12 PM

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

How to implement bubble sort algorithm in C# How to implement bubble sort algorithm in C# Sep 19, 2023 am 11:10 AM

How to implement the bubble sort algorithm in C# Bubble sort is a simple but effective sorting algorithm that arranges an array by comparing adjacent elements multiple times and exchanging positions. In this article, we will introduce how to implement the bubble sort algorithm using C# language and provide specific code examples. First, let us understand the basic principles of bubble sort. The algorithm starts from the first element of the array and compares it with the next element. If the current element is larger than the next element, swap their positions; if the current element is smaller than the next element, keep it

Analyze time complexity and space complexity in Go language Analyze time complexity and space complexity in Go language Mar 27, 2024 am 09:24 AM

Go is an increasingly popular programming language that is designed to be easy to write, easy to read, and easy to maintain, while also supporting advanced programming concepts. Time complexity and space complexity are important concepts in algorithm and data structure analysis. They measure the execution efficiency and memory size of a program. In this article, we will focus on analyzing the time complexity and space complexity in the Go language. Time Complexity Time complexity refers to the relationship between the execution time of an algorithm and the size of the problem. Time is usually expressed in Big O notation

Complexity analysis of various PHP array sorting algorithms Complexity analysis of various PHP array sorting algorithms Apr 27, 2024 am 09:03 AM

PHP array sorting algorithm complexity: Bubble sort: O(n^2) Quick sort: O(nlogn) (average) Merge sort: O(nlogn)

Java Data Structures and Algorithms: A Practical Guide to Cloud Computing Java Data Structures and Algorithms: A Practical Guide to Cloud Computing May 09, 2024 am 08:12 AM

The use of data structures and algorithms is crucial in cloud computing for managing and processing massive amounts of data. Common data structures include arrays, lists, hash tables, trees, and graphs. Commonly used algorithms include sorting algorithms, search algorithms and graph algorithms. Leveraging the power of Java, developers can use Java collections, thread-safe data structures, and Apache Commons Collections to implement these data structures and algorithms.

CS-Week 3 CS-Week 3 Apr 04, 2025 am 06:06 AM

Algorithms are the set of instructions to solve problems, and their execution speed and memory usage vary. In programming, many algorithms are based on data search and sorting. This article will introduce several data retrieval and sorting algorithms. Linear search assumes that there is an array [20,500,10,5,100,1,50] and needs to find the number 50. The linear search algorithm checks each element in the array one by one until the target value is found or the complete array is traversed. The algorithm flowchart is as follows: The pseudo-code for linear search is as follows: Check each element: If the target value is found: Return true Return false C language implementation: #include#includeintmain(void){i

See all articles