search
Homephp教程PHP开发Three implementations of bubble sort
Three implementations of bubble sortDec 19, 2016 pm 01:15 PM
Bubble Sort

Bubble sorting is very easy to understand and implement, take sorting from small to large as an example:

Suppose the array length is N.

1. Compare the two adjacent data before and after. If the former data is greater than the latter data, the two data will be exchanged.

2. In this way, after traversing the 0th data to the N-1th data of the array, the largest data will "sink" to the N-1th position of the array.

3. N=N-1, if N is not 0, repeat the previous two steps, otherwise the sorting is completed.

It is easy to write the code by definition:

//冒泡排序1  
void BubbleSort1(int a[], int n)  
{  
       int i, j;  
       for (i = 0; i < n; i++)  
              for (j = 1; j < n - i; j++)  
                     if (a[j - 1] > a[j])  
                            Swap(a[j - 1], a[j]);  
}

Let’s optimize it and set a flag, which is true if an exchange occurs on this trip, otherwise it is false. Obviously, if there is no exchange in one trip, it means that the sorting has been completed.

//冒泡排序2  
void BubbleSort2(int a[], int n)  
{  
       int j, k;  
       bool flag;  
  
       k = n;  
       flag = true;  
       while (flag)  
       {  
              flag = false;  
              for (j = 1; j < k; j++)  
                     if (a[j - 1] > a[j])  
                     {  
                            Swap(a[j - 1], a[j]);  
                            flag = true;  
                     }  
              k--;  
       }  
}

Let’s do further optimization. If there is an array of 100 numbers, only the first 10 are unordered, and the next 90 are all sorted and all are greater than the first 10 numbers, then after the first traversal, the last position where the exchange occurs must be less than 10, and this The data after the position must be in order. Record this position. The second time you just need to traverse from the head of the array to this position.

//冒泡排序3  
void BubbleSort3(int a[], int n)  
{  
    int j, k;  
    int flag;  
      
    flag = n;  
    while (flag > 0)  
    {  
        k = flag;  
        flag = 0;  
        for (j = 1; j < k; j++)  
            if (a[j - 1] > a[j])  
            {  
                Swap(a[j - 1], a[j]);  
                flag = j;  
            }  
    }  
}

Bubble sorting is, after all, an inefficient sorting method and can be used when the data size is small. When the data size is relatively large, it is best to use other sorting methods.


For more articles related to the three implementations of bubble sorting, please pay attention to the PHP Chinese website!

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
如何实现C#中的冒泡排序算法如何实现C#中的冒泡排序算法Sep 19, 2023 am 11:10 AM

如何实现C#中的冒泡排序算法冒泡排序是一种简单但有效的排序算法,它通过多次比较相邻的元素并交换位置来排列一个数组。在本文中,我们将介绍如何使用C#语言实现冒泡排序算法,并提供具体的代码示例。首先,让我们了解一下冒泡排序的基本原理。算法从数组的第一个元素开始,与下一个元素进行比较。如果当前元素比下一个元素大,则交换它们的位置;如果当前元素比下一个元素小,则保持

PHP 数组自定义排序算法的编写指南PHP 数组自定义排序算法的编写指南Apr 27, 2024 pm 06:12 PM

如何编写自定义PHP数组排序算法?冒泡排序:通过比较和交换相邻元素来排序数组。选择排序:每次选择最小或最大元素并将其与当前位置交换。插入排序:逐个插入元素到有序部分。

C++ 函数性能优化中的算法选择与优化技巧C++ 函数性能优化中的算法选择与优化技巧Apr 23, 2024 pm 06:18 PM

C++函数性能优化算法选择:选择高效算法(如快速排序、二分查找)。优化技巧:内联小型函数、优化缓存、避免深拷贝、循环展开。实战案例:查找数组最大元素位置时,优化后采用二分查找和循环展开,大幅提升性能。

各种 PHP 数组排序算法的复杂度分析各种 PHP 数组排序算法的复杂度分析Apr 27, 2024 am 09:03 AM

PHP数组排序算法复杂度:冒泡排序:O(n^2)快速排序:O(nlogn)(平均)归并排序:O(nlogn)

冒泡事件的含义是什么冒泡事件的含义是什么Feb 19, 2024 am 11:53 AM

冒泡事件是指在Web开发中,当一个元素上触发了某个事件后,该事件将会向上层元素传播,直到达到文档根元素。这种传播方式就像气泡从底部逐渐冒上来一样,因此被称为冒泡事件。在实际开发中,了解和理解冒泡事件的工作原理对于正确处理事件十分重要。下面将通过具体的代码示例来详细介绍冒泡事件的概念和使用方法。首先,我们创建一个简单的HTML页面,其中包含一个父级元素和三个子

分析 Go 语言中的时间复杂度和空间复杂度分析 Go 语言中的时间复杂度和空间复杂度Mar 27, 2024 am 09:24 AM

Go语言是一种越来越流行的编程语言,它被设计成易于编写、易于阅读和易于维护的语言,同时也支持高级编程概念。时间复杂度和空间复杂度是算法和数据结构分析中重要的概念,它们衡量着一个程序的执行效率和占用内存大小。在本文中,我们将重点分析Go语言中的时间复杂度和空间复杂度。时间复杂度时间复杂度是指算法执行时间与问题规模之间的关系。通常用大O表示法来表示时间

Python、Java和C++:哪个编程语言更值得学习?Python、Java和C++:哪个编程语言更值得学习?Mar 29, 2024 pm 02:06 PM

Python、Java和C++:哪个编程语言更值得学习?作为计算机科学领域中最常见的编程语言之一,Python、Java和C++各自具有独特的特点和优势。选择学习哪种编程语言往往取决于个人的兴趣、职业需求和项目要求。在选择编程语言时,比较它们的特性和适用场景是非常重要的。接下来将分别探讨这三种编程语言的特点,并给出相应的代码示例。Python:Python是

PHP算法:如何使用冒泡排序提高数组排序效率?PHP算法:如何使用冒泡排序提高数组排序效率?Sep 19, 2023 am 10:28 AM

PHP算法:如何使用冒泡排序提高数组排序效率?冒泡排序是一种简单但效率较低的排序算法,但我们可以通过一些优化策略提高冒泡排序的效率。本文将介绍如何使用PHP中的冒泡排序算法优化数组的排序过程,并提供具体的代码示例。冒泡排序的基本原理是,每次从数组的第一个元素开始,依次比较相邻两个元素的大小,如果前一个元素大于后一个元素,则交换它们的位置。这样一轮比较下来,最

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),