Home Backend Development PHP Tutorial Classic sorting algorithm - Bubble sort Bubble sort

Classic sorting algorithm - Bubble sort Bubble sort

Jul 29, 2016 am 09:03 AM
height margin padding px quot

经典排序算法 - 冒泡排序Bubble sort

原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,

这样一趟过去后,最大或最小的数字被交换到了最后一位,

然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子

例子为从小到大排序,

原始待排序数组| 6 | 2 | 4 | 1 | 5 | 9 |

第一趟排序(外循环)

第一次两两比较6 > 2交换(内循环)

交换前状态| 6 | 2 | 4 | 1 | 5 | 9 |

交换后状态| 2 | 6 | 4 | 1 | 5 | 9 |

第二次两两比较,6 > 4交换

交换前状态| 2 | 6 | 4 | 1 | 5 | 9 |

交换后状态| 2 | 4 | 6 | 1 | 5 | 9 |

第三次两两比较,6 > 1交换

交换前状态| 2 | 4 | 6 | 1 | 5 | 9 |

交换后状态| 2 | 4 | 1 | 6 | 5 | 9 |

第四次两两比较,6 > 5交换

交换前状态| 2 | 4 | 1 | 6 | 5 | 9 |

交换后状态| 2 | 4 | 1 | 5 | 6 | 9 |

第五次两两比较,6 < 9不交换

交换前状态| 2 | 4 | 1 | 5 | 6 | 9 |

交换后状态| 2 | 4 | 1 | 5 | 6 | 9 |

第二趟排序(外循环)

第一次两两比较2 < 4不交换

交换前状态| 2 | 4 | 1 | 5 | 6 | 9 |

交换后状态| 2 | 4 | 1 | 5 | 6 | 9 |

第二次两两比较,4 > 1交换

交换前状态| 2 | 4 | 1 | 5 | 6 | 9 | 
交换后状态| 2 | 1 | 4 | 5 | 6 | 9 |

第三次两两比较,4 < 5不交换

交换前状态| 2 | 1 | 4 | 5 | 6 | 9 | 
交换后状态| 2 | 1 | 4 | 5 | 6 | 9 |

第四次两两比较,5 < 6不交换

交换前状态| 2 | 1 | 4 | 5 | 6 | 9 |

交换后状态| 2 | 1 | 4 | 5 | 6 | 9 |

第三趟排序(外循环)

第一次两两比较2 > 1交换

交换后状态| 2 | 1 | 4 | 5 | 6 | 9 |

交换后状态| 1 | 2 | 4 | 5 | 6 | 9 |

第二次两两比较,2 < 4不交换

交换后状态| 1 | 2 | 4 | 5 | 6 | 9 | 
交换后状态| 1 | 2 | 4 | 5 | 6 | 9 |

第三次两两比较,4 < 5不交换

交换后状态| 1 | 2 | 4 | 5 | 6 | 9 | 
交换后状态| 1 | 2 | 4 | 5 | 6 | 9 |

第四趟排序(外循环)无交换

第五趟排序(外循环)无交换

贴上自己写的代码:
function bubble_sort($arr=array(1,43,54,62,21,66,32,78,36,76,39))
{  
    $len = count($arr);
    //该层循环控制 需要冒泡的轮数
    for($i=1; $i<$len; $i++)
    { //该层循环用来控制每轮 冒出一个数 需要比较的次数
        for($k=0; $k<$len-$i; $k++)
        {
           if($arr[$k] > $arr[$k+1])
            {
                $tmp = $arr[$k+1];
                $arr[$k+1] = $arr[$k];
                $arr[$k] = $tmp;
                @$arr['cly_times'] +=1; //设置循环结束
            }
        }
        if ($arr['cly_times'] == $arr['cly_times']-1) {
        	return $arr;
        }
    }
    return $arr;
}
Copy after login

The above has introduced the classic sorting algorithm - Bubble sort, including various aspects. 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 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 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)

The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element Jan 05, 2024 pm 05:41 PM

From px to rem: The evolution and application of CSS layout units Introduction: In front-end development, we often need to use CSS to implement page layout. Over the past few years, CSS layout units have evolved and developed. Initially we used pixels (px) as the unit to set the size and position of elements. However, with the rise of responsive design and the popularity of mobile devices, pixel units have gradually exposed some problems. In order to solve these problems, the new unit rem came into being and was gradually widely used in CSS layout. one

Detailed explanation of CSS border properties: padding, margin and border Detailed explanation of CSS border properties: padding, margin and border Oct 21, 2023 am 11:07 AM

CSS border properties explained in detail: padding, margin and borderCSS is a style sheet language used to control and layout web page elements. In web design, the border attribute is one of the most important parts. This article will introduce in detail how to use the border attribute in CSS and provide specific code examples. padding The padding property is used to set the padding of an element, which is the space between the element's content and the element's borders. We can set padding using positive numbers or percentage values

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What does margin mean in css What does margin mean in css Dec 18, 2023 am 10:30 AM

In CSS, margin is a property used to set the outer margins of an element. Margins are the space between an element's border and its content. Margin can accept the following values: 1. A single value: for example, margin: 10px; Set all four margins (top, right, bottom, left) to 10 pixels; 2. Two values: for example, margin : 10px 20px; Set the top and bottom margins to 10 pixels, and the left and right margins to 20 pixels; 3, four values, and so on.

Detailed explanation of CSS dimension properties: height and width Detailed explanation of CSS dimension properties: height and width Oct 21, 2023 pm 12:42 PM

Detailed explanation of CSS dimension properties: height and width In front-end development, CSS is a powerful style definition language. Among them, height and width are the two most basic dimension attributes, used to define the height and width of the element. This article will analyze these two properties in detail and provide specific code examples. 1. Height attribute The height attribute is used to define the height of an element. You can use pixel, percentage or

Detailed explanation of CSS text layout properties: text-overflow and white-space Detailed explanation of CSS text layout properties: text-overflow and white-space Oct 20, 2023 am 11:19 AM

Detailed explanation of CSS text layout properties: text-overflow and white-space In web design, text layout is a very important link. Reasonable layout can make the text more readable and beautiful. CSS provides several properties to control how text is displayed, including text-overflow and white-space. This article will detail the usage and sample code of these two properties. 1. text-overflow attribute text

How to use padding attribute in css How to use padding attribute in css Dec 07, 2023 pm 02:58 PM

In CSS, the padding property is used to set the padding of an element. This means it defines the space between the element's content and its border. The basic syntax is "padding: value;".

The margin attribute does not affect inline elements The margin attribute does not affect inline elements Feb 18, 2024 pm 04:36 PM

The effect of margin on inline elements is different from that of block-level elements. In inline elements, the margin attribute only affects the vertical top and bottom margins, not the horizontal left and right margins. For example, if there is a paragraph element in HTML, we can set some styles for it and observe the effect of the margin attribute on it. The HTML code looks like this:

See all articles