Another PHP bubble sorting algorithm, php bubble sorting algorithm _ PHP Tutorial

Source: Internet
Author: User
Another PHP bubble sorting algorithm, php bubble sorting algorithm. Another bubble sorting algorithm implemented by PHP is shared. the classic bubble sorting method of the php bubble sorting algorithm has always been one of the methods used by many programs, in other words, the efficiency of the bubble sorting method is higher than that of another PHP bubble sorting algorithm.

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

Bubble sorting means to compare two adjacent numbers in sequence, then sort them by size until the last two digits. Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting. But in fact, you can also use it based on your own needs. Place the big tree forward and place the decimal number backward.

<? Php/*** use the bubble sorting method in PHP * // pre-declare an array $ arr = array (,); echo "original array "; print_r ($ arr); echo"
"; // Bubble sort function maopao ($ arr) {// traverse the first layer for ($ I = 0, $ k = count ($ arr ); $ I <$ k; $ I ++) {// perform layer 2 traversal. compare each element in the array with the outer element. // I + 1 indicates that the outer element traverses the for ($ j = $ I + 1; $ j <$ k; $ j ++) {// compare the two numbers of internal and external layers if ($ arr [$ I] <$ arr [$ j]) {// assign an array to the temporary variable $ temp = $ arr [$ j]; // The switching position $ arr [$ j] = $ arr [$ I]; // assign a value from the temporary variable $ arr [$ I] = $ temp ;}}// return the sorted array return $ arr ;} // Print the sorted array echo 'sorted 'directly; print_r (maopao ($ arr);?>

Use the above code to execute the result

Original array

The code is as follows:

Array ([0] => 12 [1] => 45 [2] => 28 [3] => 30 [4] => 88 [5] => 67)


After sorting

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! There is no 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 a bubble sorting algorithm for me ??

# Include
# Include
# Define M 10
Using namespace std;
Void maopao1 (int data [M])
{
Int I, j, t;
For (I = 1; I <= M-1; I ++) // compare times of external loop control
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 ++) // compare times of external loop control
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 <"enter" < For (I = 0; I Cin> data [I];
Maopao1 (data); // from small to large
Maopao2 (data); // from large to small

System ("pause ");
Return 0;
}

The classic bubble sorting method of sort has always been one of the methods used by many programs. In other words, bubble sorting method is more efficient...

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.