Home > Backend Development > PHP Tutorial > Bubble Sort, bubblesort_PHP tutorial

Bubble Sort, bubblesort_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:14:49
Original
892 people have browsed it

Bubble Sort, bubblesort

8 numbers. Sort as ascend.

1st loop, compare 7 times (for 8 numbers), and found the largest 8.

2nd loop, compare 6 times (for 7 numbers), and found the largest 7.

. . .

1, 7, 8

2, 6, 7

3, 5, 6

4, 4, 5

5, 3, 4

6, 2, 3

7, 1, 2

In conclusion: For sorting 8 numbers, we need an outer loop of 7 times, each time for finding a largest number; and an inner loop from comparing 7 times to comparing 1 time (as in the center column).

Implementation in PHP:

<span> 1</span> <?<span>php
</span><span> 2</span> <span>/*</span><span> bubble sort: 
</span><span> 3</span> <span>    1. operate directly on the input array (&), not on a copy
</span><span> 4</span> <span>    2. sort as ascend
</span><span> 5</span> 
<span> 6</span> <span>    a is array
</span><span> 7</span> <span>    m is length of a
</span><span> 8</span> <span>    n is times of outer loop, n-i is times of comparing for each outer loop
</span><span> 9</span> <span>    i/j is for-loop counter
</span><span>10</span> <span>    w is for value swap
</span><span>11</span> <span>*/</span>
<span>12</span> <span>function</span> sortBubble(&<span>$a</span><span>){
</span><span>13</span>     <span>$m</span> = <span>count</span>(<span>$a</span><span>);
</span><span>14</span>     <span>$n</span> = <span>$m</span> - 1<span>;
</span><span>15</span>     <span>for</span>(<span>$i</span>=0; <span>$i</span><<span>$n</span>; <span>$i</span>++<span>){
</span><span>16</span>         <span>for</span>(<span>$j</span>=0; <span>$j</span><<span>$n</span>-<span>$i</span>; <span>$j</span>++<span>){
</span><span>17</span>             <span>if</span>(<span>$a</span>[<span>$j</span>] > <span>$a</span>[<span>$j</span>+1<span>]){
</span><span>18</span>                 <span>$w</span> = <span>$a</span>[<span>$j</span><span>];
</span><span>19</span>                 <span>$a</span>[<span>$j</span>] = <span>$a</span>[<span>$j</span>+1<span>];
</span><span>20</span>                 <span>$a</span>[<span>$j</span>+1] = <span>$w</span><span>;
</span><span>21</span> <span>            }
</span><span>22</span>             <span>else</span><span>{
</span><span>23</span>                 <span>//</span><span> do nothing</span>
<span>24</span> <span>            }
</span><span>25</span> <span>        }
</span><span>26</span>         <span>//</span><span> see the results after each outer loop
</span><span>27</span> <span>        // echo implode(', ', $a).'<br />';</span>
<span>28</span> <span>    }
</span><span>29</span> <span>}
</span><span>30</span> 
<span>31</span> <span>$arr</span> = <span>array</span>(9, 5, 2, 7, 3<span>);
</span><span>32</span> sortBubble(<span>$arr</span><span>);
</span><span>33</span> <span>echo</span> <span>implode</span>(', ', <span>$arr</span><span>);
</span><span>34</span> 
<span>35</span> <span>//</span><span> 2, 3, 5, 7, 9</span>
<span>37</span> ?>
Copy after login

Bubble sort c

Detailed notes on bubble sorting:
/* Use bubble sorting method to sort ten numbers in a one-dimensional integer array in ascending order*/
#include
#include

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n") ;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* Risk Bubble sorting*/
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{t=a[ j];/* Swap a[i] and a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;i<10;i++)
printf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
When i=0:
j starts from 0 a[0],a [1] Compare the sizes, give the larger one to a[1], then compare j++, a[1] and a[2], and then give the larger
of the two to a[2] , In this way, the largest of a[0], a[1], a[2] has been exchanged into a[2], and this process continues until j=10-i-1=9 like this
a[9 ] is the largest number among the 10 numbers.
Then when i=1:
Since the maximum number has been found and placed in a[9], this time the loop j only needs to reach a maximum of 10-i-1=8, that is, a[8]. , starting from j=0 again, a[j] and a[j+1] are compared and exchanged in pairs, and the last large number is placed in a[8]
Then i++, continue...
When i= At 9 o'clock, 9 pairwise comparisons have been made to complete all sorting, and i<9 is no longer true and the comparison is exited.
For n numbers, only n-1 pairwise comparisons in the outer loop are needed to complete the sorting.
As for sorting in descending order, just change if(a[j]>a[j+1]) to if(a[j]
-------------------------------------------------- -----------------------
/* Use the improved bubble sort method to sort the ten numbers in the one-dimensional integer array in ascending order*/
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i =0;i<9;i++) /* Improved bubble sorting*/
{ flag=0;
for(j=0;j<10-i-1;j++)
if (a[j]>a[j+1])
{ t=a[j]; /* Swap a[i] and a[j] */
a[j]=a[j +1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&...the rest of the text>>

Bubble sort c

Detailed notes on bubble sorting:
/* Use bubble sorting method to sort ten numbers in a one-dimensional integer array in ascending order*/
#include
#include

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n") ;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* Risk Bubble sorting*/
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{t=a[ j];/* Swap a[i] and a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;i<10;i++)
printf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
When i=0:
j starts from 0 a[0],a [1] Compare the sizes, give the larger one to a[1], then compare j++, a[1] and a[2], and then give the larger
of the two to a[2] ,In this way, the largest of a[0],a[1],a[2] has been exchanged into a[2], and this process continues until j=10-i-1=9 like this
a[9 ] is the largest number among the 10 numbers.
Then when i=1:
Since the maximum number has been found and placed in a[9], this time the loop j only needs to reach a maximum of 10-i-1=8, that is, a[8]. , starting from j=0 again, a[j] and a[j+1] are compared and exchanged in pairs, and the last large number is placed in a[8]
Then i++, continue...
When i= At 9 o'clock, 9 pairwise comparisons have been made to complete all sorting, and i<9 is no longer true and the comparison is exited.
For n numbers, only n-1 pairwise comparisons in the outer loop are needed to complete the sorting.
As for sorting in descending order, just change if(a[j]>a[j+1]) to if(a[j]
-------------------------------------------------- -----------------------
/* Use the improved bubble sort method to sort the ten numbers in the one-dimensional integer array in ascending order*/
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i =0;i<9;i++) /* Improved bubble sorting*/
{ flag=0;
for(j=0;j<10-i-1;j++)
if (a[j]>a[j+1])
{ t=a[j]; /* Swap a[i] and a[j] */
a[j]=a[j +1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&...the rest of the text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/908125.htmlTechArticleBubble Sort, bubblesort 8 numbers. Sort as ascend. 1st loop, compare 7 times (for 8 numbers), and found the largest 8. 2nd loop, compare 6 times (for 7 numbers), and found the lar...
Related labels:
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