Another PHP implementation of the Bubble sorting algorithm sharing, PHP bubble sorting algorithm _php tutorial

Source: Internet
Author: User

Another PHP implementation of the Bubble sorting algorithm sharing, PHP bubble sorting algorithm


The classic bubble sorting method has always been one of many programs, and the bubbling sort method is more efficient than the PHP system function sort. This chapter does not discuss performance, so do not compare it to the system capability.

The bubble sort probably means to compare the adjacent two numbers in turn, then sort by size until the last two digits. Since the decimal is always placed forward in the sorting process, the large number is placed backwards, which is the equivalent of bubbles rising, so called bubble sort. But in fact in the actual process can also be in accordance with their own needs in turn, the big tree forward, the decimal place.

<?php/** * The bubbling Sort method in PHP uses *////Pre-declares an array $arr = array (12,45,28,30,88,67); echo "original array";p Rint_r ($arr); echo "
";///bubble sort function Maopao ($arr) { //Perform first-level traversal for ($i =0, $k =count ($arr); $i < $k; $i + +) { //second-level traversal Compares each element in the array with the outer element //Here the i+1 means that the outer loop iterates through the current element for the next ($j = $i +1; $j < $k; $j + +) { ///inner/Outer Two number comparison if ($arr [$i] < $arr [$j]) { //Assign one of the arrays to a temporary variable $temp = $arr [$j]; Swap position $arr [$j] = $arr [$i]; Then assign a value back from the temporary variable $arr [$i] = $temp;}} } Returns the sorted array return $arr;}//directly prints the sorted array after echo ' sort ';p rint_r (Maopao ($arr));?>

Execute results from the above code

Original array
Copy the code as follows: Array ([0] = [1] = [2] = [3] = [4] = [5] = 67)
After sorting
Copy the code as follows: Array ([0] = [1] = [2] = [3] = [4] = [5] = 12)
This is the Bubbling Method example, simple! Without the difficulty of God horse.


Programming to implement a bubbling sorting 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 give me a program to implement a bubble sorting algorithm??

#include
#include
#define M
using namespace std;
void maopao1 (int data[m])
{
int i,j,t;
for (i=1;i<=m-1;i++)//Outer loop control compare number
for (j=0;j if (data[j]>data[j+1])
{t=data[j];d ata[j] =DATA[J+1];d ata[j+1]=t;}
cout<< "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 compare number
for (j=0;j if (Data[j] {t=data[j];d Ata[j]=d ATA[J+1];d ata[j+1]=t;}
cout<< "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 large to small

System ("pause");
return 0;
}
 

http://www.bkjia.com/PHPjc/867250.html www.bkjia.com true http://www.bkjia.com/PHPjc/867250.html techarticle another PHP implementation of the Bubble sorting algorithm sharing, PHP bubble sorting algorithm Classic bubble sorting method has been a lot of procedures used in one of the sorting method, saying that the bubble sorting method in efficiency than ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.