Home > Article > Backend Development > Bubble Sort, bubblesort_PHP tutorial
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> ?>
Detailed notes on bubble sorting:
/* Use bubble sorting method to sort ten numbers in a one-dimensional integer array in ascending order*/
#include ade979de5fc0e1ca0540f360a64c230b
#include 8e359799bdf1a571032ba13cc96acda9
int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n") ;
for(i=0;if8e795f37c74cf495912ea20a2670173a[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;i222b331eda818bf3cb007999f0d3299ea[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 ade979de5fc0e1ca0540f360a64c230b
#include 8e359799bdf1a571032ba13cc96acda9
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;iabb56720dae1f92044dc5875fc4762bba[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>>
Detailed notes on bubble sorting:
/* Use bubble sorting method to sort ten numbers in a one-dimensional integer array in ascending order*/
#include ade979de5fc0e1ca0540f360a64c230b
#include 8e359799bdf1a571032ba13cc96acda9
int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n") ;
for(i=0;if8e795f37c74cf495912ea20a2670173a[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;i10701802b9c912c1e91019659e14a393a[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 ade979de5fc0e1ca0540f360a64c230b
#include 8e359799bdf1a571032ba13cc96acda9
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;iabb56720dae1f92044dc5875fc4762bba[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>>