Home > Backend Development > PHP Tutorial > Another bubble sorting algorithm implemented in PHP to share, php bubble sorting algorithm_PHP tutorial

Another bubble sorting algorithm implemented in PHP to share, php bubble sorting algorithm_PHP tutorial

WBOY
Release: 2016-07-13 10:20:20
Original
1810 people have browsed it

Another bubble sorting algorithm implemented in PHP to share, php bubble sorting algorithm

The classic bubble sorting method has always been one of the sorting methods used by many programs. It is said that the bubble sorting method is more efficient than the PHP system function sort. This chapter does not discuss performance, so we will not compare it with system performance.

Bubble sorting roughly means comparing two adjacent numbers in sequence, and then sorting according to size until the last two digits. Since in the sorting process, small numbers are always placed forward and large numbers are placed backward, which is equivalent to bubbles rising, so it is called bubble sorting. But in fact, in the actual process, you can also use it in reverse according to your own needs. The big trees are placed forward and the decimals are placed backward.

<&#63;php
/**
 * PHP中的冒泡排序法使用
 */
 
// 预先声明一个数组
$arr = array (12,45,28,30,88,67);
echo "原数组";
print_r($arr);
echo "<br/>";
//冒泡排序
function maopao($arr){
  // 进行第一层遍历
  for($i=0,$k=count($arr);$i<$k;$i++) {
    // 进行第二层遍历 将数组中每一个元素都与外层元素比较
    // 这里的i+1意思是外层遍历当前元素往后的
    for ($j=$i+1;$j<$k;$j++) {
      // 内外层两个数比较
        if($arr[$i]<$arr[$j]){
        // 先把其中一个数组赋值给临时变量
          $temp = $arr[$j];
        // 交换位置
        $arr[$j] = $arr[$i];
        // 再从临时变量中赋值回来
        $arr[$i] = $temp;
      }
    }
  }
  // 返回排序后的数组
  return $arr;
}
 
// 直接打印排序后的数组
echo '排序后';
print_r(maopao($arr));
 
&#63;>
Copy after login

Execute the results through the above code

Original array

Copy code The code is as follows:
Array ( [0] => 12 [1] => 45 [2] => 28 [ 3] => 30 [4] => 88 [5] => 67 )

After sorting
Copy code The code is as follows:
Array ( [0] => 88 [1] => 67 [2] => 45 [ 3] => 30 [4] => 28 [5] => 12 )

This is an example of the bubble method, simple! There is no magic difficulty.

Programming to implement a bubble sort algorithm?

int [] array = new int
;
int temp = 0 ;
for (int i = 0 ; i < array.Length - 1 ; i++)
{
for (int j = i + 1 ; j < array.Length ; j++)
{
if (array[j] < array[i])
{
temp = array[i ] ;
array[i] = array[j] ;
array[j] = temp ;
}
}
}

Can you program me a bubble sort algorithm? ?

#include
#include
#define M 10
using namespace std;
void maopao1(int data[M])
{
int i,j,t;
for(i=1;i<=M-1;i++)//Outer loop control number of comparisons
for(j=0;j if(data[j]>data[j+1])
{t=data[j];data[j]=data[j+1];data[j+1]=t;}
cout<<"After sorting from small to large:"< for(i=0;i cout< cout<}
void maopao2(int data[M])
{
int i,j,t;
for(i=1 ;i<=M-1;i++)//Outer loop control number of comparisons
for(j=0;j if(data[j] {t=data[j];data[j]=data[j+1];data[j+1]=t;}
cout<<"After sorting from small to large:"< for(i=0;i cout< cout<}
int main()
{
int i,data[M];
cout<<"Please enter "< for(i=0;i cin>>data[i];
maopao1(data);//from small to large
maopao2(data );//From big to small

system("pause");
return 0;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/867250.htmlTechArticleAnother bubble sorting algorithm implemented in PHP to share, php bubble sorting algorithm The classic bubble sorting method has always been One of the sorting methods used by many programs. It is said that bubble sorting is more efficient than...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template